JB Header
'@Override annotation in Java - tutorial with examples
This tutorial explains @Override annotation in Java. It starts off with giving a little background on overriding and inheritance in OOPS. It then describes usage of @Override annotation with examples. Next you will learn about how the compiler complains when you don't override correctly while using this annotation. Finally, the benefits of using the @Override annotation are explained.
Background of @Override annotation Inheritance and polymorphism are essential to any object oriented programming, or OOP, language. One of the core aspects of inheritance is the ability to override methods in the base class. However, one needs to take care while defining the overridden method as even a small misplacement of a parameter in the overriding method or an incorrect return type can result in a new method definition rather than an overridden method.
To prevent such inadvertent and unintentional definition of new methods when the intention is actually to override an existing method, @Override annotation was introduced in Java 1.5 for a derived class overriding its base class's methods. From Java 1.6 onward this feature was extended for implementation classes overriding methods of the interfaces they implement.
@Override annotation basics @Override annotation is a predefined annotation present in java.lang package and is used to annotate the . This annotation enforces the necessary matching of the method definitions of the overriding method in the derived class with that of the overridden method in the base class. I.e. if the overriding method's signature does not match that of the overridden method then a compilation error is thrown.
Let us now look at an example scenario to see how @Override annotation is to be used.

@Override Implementation Example
public class BaseClass {
  public int baseClassMethod(int param){
    return 0;
  }
}

public interface BaseInterface {
  public int baseInterfaceMethod(int param);	
}

public class DerivedClass extends BaseClass 
		    implements BaseInterface{
  @Override
  public int baseClassMethod(int param){
    return 1;
  }
	
  @Override
  public int baseInterfaceMethod(int param){
    return 2;
  }
}
Quick explanation of the above code
  • BaseClass is the parent class containing a method baseClassMethod.
  • BaseInterface is the parent interface containing a method baseInterfaceMethod.
  • DerivedClass both extends BaseClass and implements BaseInterface.
  • DerivedClass overrides baseClassMethod as well as baseInterfaceMethod.
  • Notice the @Override annotation defined on top of both the overriding methods in DerivedClass.
Compiler error thrown when incorrectly overriding using @Override annotation If you use the @Override annotation in the derived class over an overriding method and do not use the same method definition as that of the parent class method then you will get a compile time error.

Lets say you have BaseClass and DerivedClass defined like this -
@Override Implementation Compilation Error Scenario
public class BaseClass {
  public int baseClassMethod(int param){
    return 0;
  }
}

public class DerivedClass extends BaseClass {
  @Override
  public int baseClassMethod(String param){
    return 1;
  }
}
The compile time error you will get for the above code is -
The method baseClassMethod(String) of type DerivedClass must override or implement a supertype method

To fix this compiler error, you will need to change the definition of baseClassMethod in DerivedClass to -
public int baseClassMethod(String param) 
@Override Annotation benefits
  1. Ensures correct overriding - Many-a-times while overriding a method, some inadvertent mistakes creep in such as incorrect capitalisation in the method name, wrong parameter types or missing a parameter, incorrect return parameter type etc. When we use @Override annotation we get to know of this mistakes at compile time itself. This saves the effort of running the code and then debugging to find out the source of the problem.
  2. Improves readability of the code - When a developer needs to work on the code written by someone else, @Override increases the readability of the code manifold as one can immediately identify overridden methods and then try to understand which APIs or logic they help accomplish.
  3. Ensures future API design changes do not break dependent implementation classes - Often as code ages and increases in size and complexity, it becomes difficult to remember the dependencies between classes. Using @Override annotation in derived classes ensures that whenever the method definitions change in the parent classes or interfaces, the compiler ensures that all the places where the changed methods are being overridden in the code show compiler error. And until all such error instances are taken care of, and design consistencies restored, the application doesn't get compiled and built again.
Summary - In this article we had a look at @Override annotation basics which included explanation of its usage with examples. We then looked at the compiler error thrown which checks for incorrect overriding when you use this annotation.Lastly, we looked at the benefits that you get from using @Override annotation. If you think there's something which is not clearly explained or needs to be included in this tutorial then do leave your comments below.