The @Override annotation is a standard Java annotation that was first introduced in Java 1.5. The @Override annotation denotes that the child class method overrides the base class method.
For two reasons, the @Override annotation is useful.
- If the annotated method does not actually override anything, the compiler issues a warning.
- It can help to make the source code more readable.
Why we use @Override annotation:
Because of the following two advantages, using the @Override annotation when overriding a method is considered a best practice for coding in Java:
1) You’ll get a compile-time error if the programmer makes a mistake while overriding, such as using the wrong method name or parameter types. Because you are informing the compiler that you are overriding this method by using this annotation. If you don’t use the annotation, the sub-class method will be treated as a new method in the subclass (rather than the overriding method).
2) It improves the code’s readability. If you change the signature of an overridden method, all sub-classes that override it will throw a compilation error, which will eventually lead to you changing the signature in the subclasses. If you have a large number of classes in your application, this annotation will greatly assist you in identifying the classes that need to be changed when a method’s signature is changed.
Syntax:
public @interface Override
Example 1: Without usage of abstract class
Java
// Java Program Illustrating Override Annotation // Importing input output classes import java.io.*; // Class 1 // Parent class class ParentClass { // Method inside parent class public void display() { // Print statement whenever // method of parent class is called System.out.println( "We are in base class method" ); } } // Class 2 // Child class class ChildClass extends ParentClass { // @Override // Method inside child class public void display() { // Print statement whenever // method of child class is called System.out.println( "We are in child class method" ); } } // Class 3 // OverrideAnnotationTest public class GFG { // Main driver method public static void main(String args[]) { // Display message only System.out.println( "Example of @Override annotation" ); // Creating an object of parent class // with reference to child class ParentClass obj = new ChildClass(); // Calling the method to execute inside classes obj.display(); } } |
Output:
Example 2: Using the abstract class
Java
// Java Program illustrating Override Annotation // Using Abstract class // Importing input output classes import java.io.*; // Class 1 // Helper abstract class abstract class Vehicle { // Calling this method public abstract void method(); } // Class 2 // Helper class class Car extends Vehicle { // @Override // Method of Car class public void method() { // Print statement whenever this method is called System.out.println( "This is Car" ); } } // Class 3 // Helper class class Bike extends Vehicle { // @Override // Method of bike class public void method() { // Print statement whenever this method is called System.out.println( "This is Bike" ); } } // Class 4 // OverrideAnnotationExample public class GFG { // Main drive method public static void main(String[] args) { // Creating object of both the classes // namely Car and Bike Car Carobj = new Car(); // Calling method over car object Carobj.method(); Bike Bikeobj = new Bike(); // Similarly calling method over bike object Bikeobj.method(); } } |
This is Car This is Bike