@Override on implementation

Do you put annotation in the methods of the implementation class? Does it serve any purpose? If you are mistaken or do not have it, it is still a compilation error.

+3
source share
6 answers

I assume that you are asking about annotation methods that are defined in the implemented interface, or as abstractin a superclass. In this case, you are correct that a typo in the method signature will result in a compilation error with or without @Override. However, I think the annotation is still useful to explicitly mark a method as an implementation of an interface. If the interface ever changes, @Overrideannotations for all implementation methods can help determine which method signatures have been changed and need to be updated.

More importantly, as mklhmnn mentioned in his answer, if the method is removed from the interface, the annotation @Overridein your implementation class will cause a compilation error. Without annotation, you may not know that the method has been removed from the interface, which can lead to subtle errors.

+3
+10

. , @Override, , .

, Foo :

@Override 
public boolean equals (Foo foo) { ... }

, , equals (Foo foo) - Foo.

IDE (, IntelliJ) , IDE.

+4

. , (Eclipse?, ).

, IDE .

@Override , , , HUMANS , , . IDE , , , , .

+2

@Override , API , - . , .

+1

, , Javadoc , .

+1

Source: https://habr.com/ru/post/1756416/


All Articles