The @Override Annotation in Java (original) (raw)

Last Updated : 15 Jun, 2026

The @Override annotation is a predefined annotation in Java that indicates a method in a subclass is intended to override a method from its superclass or an implemented interface. It was introduced in Java 5 and helps the compiler verify that method overriding is performed correctly.

**Syntax:

@Override
returnType methodName(parameters) {
// method body
}

**Example: Java Program Illustrating Override Annotation without using abstract class

Java `

// Importing input output classes import java.io.*;

// Class 1 // Parent class class ParentClass { @Override // 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 of @Override annotation We are in child class method

**Explanation: The ChildClass inherits from ParentClass and overrides the display() method. When obj.display() is called using a parent reference, the overridden method in ChildClass executes, demonstrating method overriding and runtime polymorphism.

**Example: Java Program Illustrating Override Annotation with using abstract class

Java `

// 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();
}

}

`

Output

This is Car This is Bike

**Explanation: The abstract class Vehicle declares the abstract method method(). The Car and Bike classes override this method and provide their own implementations. When the method is called, the respective class-specific implementation is executed.

Why Use @Override Annotation?

Advantages of @Override Annotation