Why use @Override annotation in Java Tutorial with Examples

When we use @Override annotation to a method, it let compiler know that we are overriding it from super class. It is not mandatory to use @Override when you override a method. Incase you are using @Override annotation, and the method signature is not found at super class will results compilation error.

@Override annotation was added in JDK 1.5 and it is used to instruct compiler that method annotated with @Override is an overridden method from super class or interface. Though it may look trivial @Override is particularly useful while overriding methods which accept Object as parameter just like equals, compareTo or compare() method of Comparator interface.

One of the major problem with @Override annotation on JDK 1.5 was that it can only be used to in conjunction with super class i.e. compiler throws error if you use @Override annotation with interface method. From Java 6 onwards you can use @Override annotation while implementing interface method as well. This provides robust compile time checking of overriding.

@Override annotation example

Let’s see how we normally override a method in java.
We have a superclass Vehicle.java as below.
package com.tutorialsdesk.annotations;

public class Car extends Vehicle{
 public void make(String make){
  System.out.println("Car Make : "+make); 
 }
}


Now we will create a subclass Car overriding Vehicle make method.
package com.tutorialsdesk.annotations;

public class Car extends Vehicle{
 public void make(String make){
  System.out.println("Car Make : "+make); 
 }
}


Now let’s create a TestAnnotation class to check how overriding works in java.
package com.tutorialsdesk.annotations;

public class TestAnnotation {

 public static void main(String[] args) {         
  Vehicle vehicle = new Car();         
  vehicle.make("Honda");     
  } 
}


Output of the above program is:
Car Make : Honda


Here “vehicle” is of type Vehicle but at runtime it’s object of Car, so when we invoke it’s make(String str) method, it looks for the method in Car and hence the output. Now let’s change the Vehicle make method like below.
package com.tutorialsdesk.annotations;

public class Vehicle {
 public void make(Object make){
  System.out.println("Vegicle Make : "+make); 
 }
}


You will notice that compiler won’t throw any warnings/errors and now if you run the TestAnnotation program output will be;
Vegicle Make : Honda


The reason is that Vehicle make(Object str) method is not overridden anymore by Car and hence it’s invoking Vehicle implementation. Car is overloading the make() method in this case.

If you will use the @Override annotation in Car class
package com.tutorialsdesk.annotations;

public class Car extends Vehicle{
 @Override
 public void make(String make){
  System.out.println("Car Make : "+make); 
 }
}


you will get following compile time error message:
The method make(String) of type Car must override or implement a supertype method


It’s clear that using Override annotation will make sure any Superclass changes in method signature will result in a warning and you will have to do necessary changes to make sure the classes work as expected.

It’s better to resolve potential issues at Compile time than rumtime. So always use @Override annotation whenever you are trying to override a superclass method.

When to use @Override annotation

  1. When a class method is overriding a super-class method.
  2. When a class method is implementing an interface method.
  3. When an interface method respecifying a super-interface method.
Only place, where you don't want to use @Override is when the parent method is @Deprecated.

you can read here in detail about Annotations in java.

NEXT READ Java Custom Annotation and Parsing using Reflection.

Why use @Override annotation in Java Tutorial with Examples
Hope we are able to explain you Importance of Java @Override Annotation, if you have any questions or suggestions please write to us using contact us form.(Second Menu from top left).

Please share us on social media if you like the tutorial.
SHARE
    Blogger Comment
    Facebook Comment