@Override is required in Java?

Asked

Viewed 1,011 times

27

If I have an abstract class Pessoa with an abstract method lerNome when I’m implementing this method in my class João I must make use of @Override in lerNome? The same happens when I use interfaces?

  • 1

    Not a duplicate of http://answall.com/questions/22913/qual-a-finalidade-da-override?

  • I did not remember that there was this other question. It is certainly on the same subject but more comprehensive. I don’t know if you’re exactly a duplicate, you might. Let’s see what other people think.

  • Yeah, I saw that it’s more comprehensive, if it’s not duplicate I think it’s cool that he read it to go deeper.

  • I don’t think it’s duplicate. Even the chosen answer to the linked question is incomplete because it treats the annotation @overrideonly as a recur to avoid errors, which is not the case.

  • 3

    @Caffe, Huh?!? As far as I know it is only a resource to avoid mistakes... If you can indicate the source of this information I want to read, because I did not know it...

  • 1

    @Wakim What I mixed was Java with C#. In C# you have the option to hide or actually overwrite the parent class method, and overwriting is only possible if the method is declared as Virtual. If you don’t explicitly overwrite, you’re just hiding, and you can still use an explicit statement to hide. While in C# methods are sealed by default, in Java all methods are virtual by default - and this part of Java I didn’t know until now!

  • @Caffé, it’s okay, I didn’t know that the C# had this behavior, I don’t know it. Thank you for explaining, sorry anything :)

  • @user11504 if any answer answered what you wanted, you can mark it as accepted. See how on [tour].

Show 3 more comments

3 answers

24

Yes, you should do this for the compiler to check and avoid overwriting the wrong method (maybe with the wrong signature), "hiding" the correct method and possibly calling the method in the wrong class. With it you avoid the misunderstanding by a typo, for example. With it you will receive one Warning indicating the problem.

How is "just" a Warning no obligation to actually solve the problem for the Compile program, but as always one should consider warnings as if they were mistakes, let’s say it is "morally" mandatory. Anyway it is easier to maintain a code with the explicit information that your intention is to override the method.

This goes for methods declared in interfaces as well.

More information on why this annotation exists and how it works can be obtained in that reply.

3

Both in classes that implement the methods of an interface and in subclasses that subscribe to methods in superclasses, this annotation is not mandatory, but it informs the compiler that the "annotated" method subscribes to a method from its superclass (including interfaces).

Reference (very objective):

http://docs.oracle.com/javase/7/docs/api/java/lang/Override.html

2

Is recommended, non-compulsory, because if you work with JREs legacy as the 1.4 or earlier, there are no annotations, so in this case you should not use @Override, it’s just a convention to facilitate identification of superscript methods, nothing more.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.