dagblog.
← Archive

Why Use the @Override Annotation?


One of the most useful annotations Java 1.5 introduced is undoubtedly @Override. Use it whenever you override a method declared by a superclass. If the compiler can't find a method with the same signature in the superclass, it reports a compilation error straight away. Some people expect only a warning. The annotation protects your code from bugs that can be hard to detect.

public class Predek {

 public void doSomething() {

 }

}



public class Potomek extends Predek {



 @Override

 public void doSomething() { 

 }



}

Suppose the method signature changes because you add a parameter, either in the superclass or in the subclass. In Eclipse, the compiler reports “The method doSomething() of type Potomek must override or implement a supertype method.”

Medium version

Related articles

Related CZ Podcast episodes