'@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.
Quick explanation of the above code
Lets say you have BaseClass and DerivedClass defined like this -
The compile time error you will get for the above code is -
To fix this compiler error, you will need to change the definition of
@Override Annotation benefits
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.
@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;
}
}
BaseClass
is the parent class containing a methodbaseClassMethod
.BaseInterface
is the parent interface containing a methodbaseInterfaceMethod
.DerivedClass
both extendsBaseClass
and implementsBaseInterface
.DerivedClass
overridesbaseClassMethod
as well asbaseInterfaceMethod
.- Notice the
@Override
annotation defined on top of both the overriding methods inDerivedClass
.
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 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)
- 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.
- 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.
- 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.