Use of @Override annotation in java / How to use @Override annotation in java / Why should you use @Override annotation

Meaning of @Override annotation

@Override annotation is used over methods defined in a class. When this annotation is written over a method, then this method :

  1. Might be declared in a super class and defined in sub-class.
  2. Might be defined in a super(or parent) class and re-defined in sub-class(overridden).
  3. Might be declared in an interface and defined in the class implementing the interface.

Example of this annotation is given below.

public abstract class ParentClass {
   
  /*
    *  Implemented method in parent class
   */
   public void implementedMethod() {
       System.out.println("Parent class method");
   }


  /*
    *  Abstract method in parent class
   */
   public void declaredMethod();
}


public class ChildClass extends ParentClass {

  /*
   * Overridden method of parent class
   */ 
   @Override
   public void implementedMethod() {
       System.out.println("Parent class method re-implemented");
   }

  /*
    *  Parent class method implemented
   */
   @Override
   public void declaredMethod() {
       System.out.println("Parent class method implemented");
   }
}

In the above code example, child class contains two methods annotated with @Override annotation. One method is re-implemented in child class and other method was abstract in parent class but implemented in child class.

Use of @Override annotation

When @Override annotation is applied over a method, it tells the compiler and someone who reads the code that this method is an overridden method and it is either defined in a super class or an interface which the class has implemented.

It is NOT mandatory to apply this annotation over an overridden method but using it provides compile time checks. By compile time checks, means that when compiler sees this annotation, it confirms the following :

  1. Method is declared either in the super class or an interface which this class implements. Example, @Override annotation is applied over a method which does not exist in super class nor any interface and hence the compiler shows the error.
  2. Method signature of annotated method matches with that of super class or interface method declaration. Below example shows a method defined in a super class and redefined in child class annotated with @Override. Child method takes a string argument but parent method does not and hence the compiler flags error. Note that if the annotation is removed, there will be no error.

Why should @Override annotation be used

@Override annotation is used over methods which are redefined or overridden in a class. This means that they might have been defined in a super class or declared in an interface. Using this annotation provides following benefits :

               1. Improves code readability. One can easily figure out that this method also exists in a super class or interface, as mentioned earlier.

               2. Provides compile time checks so that code does not break during deployment. Example, if you have implemented a method in an      implementation class but forgot to declare it in the interface of this class, then if you mark it with @Override annotation, then you will get an error at development time only rather than at deployment stage.

               3. Ensures proper overriding. Applying @Override annotation forces the compiler to check if the arguments of the overridden method(number and their type) match with that of super class or interface method. Also, whether its return type and access modifier matches with that of super class or interface method. Thus, proper overriding is ensured.

If this annotation is missing and the method signature of the overridden method(in child class) does not match with super class of interface method, then it will be considered as a separate(or new) method which is defined in child class but is not present in parent class.

               4. Prevents code break due to maintenance activities. Suppose a method defined in a super class is overridden in a sub-class. Tomorrow, the method signature of super class changes to include one more argument. If the sub-class method has @Override annotation, you will get compiler error and hence code can be corrected. In absence of this annotation, again there will be no compile time error and the method in sub-class will be a separate method which may result in runtime errors.

Leave a Reply