Do I need to put @Override when implementing the method of the interface?

Asked 1 years ago, Updated 1 years ago, 96 views

Do I need to put @Override when implementing the method of the interface?

Read about @Override in the Java document.

"@Override indicates that when defining a method, it overrides a higher-class method. If you have @Override and you haven't overridden a higher-class method, the compiler will send an error message."

That's what it says. I don't think the interface is super class, is it not?

oop java interface annotations

2022-09-22 11:54

1 Answers

Put @Override on it if you can. That way, you can prevent the same mistakes below

class C {
    @Override
    public boolean equals(SomeClass obj){
        // // code ...
    }
}

The above code cannot be compiled because it does not properly override public boolean equals(Objectojb).

You should use @Override when implementing methods in interface (java 1.6 or higher) or when overriding methods in higher classes.


2022-09-22 11:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.