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.
- Ensures correct method overriding.
- Helps detect mistakes during compilation.
- Works with inheritance and interfaces.
Syntax:
@Override
returnType methodName(parameters) {
// method body
}
Example: Java Program Illustrating Override Annotation without using abstract class
// 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
// 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?
- Provides Compile-Time Checking: The compiler verifies that the method actually overrides a parent class or interface method.
- Prevents Coding Errors: Detects mistakes such as incorrect method names, return types, or parameter lists.
- Improves Readability: Clearly indicates that a method is overriding an inherited method.
- Makes Maintenance Easier: If the parent method signature changes, the compiler reports errors in overridden methods.
- Enhances Code Quality: Encourages proper implementation of inheritance and polymorphism concepts.
- Helps Team Collaboration: Makes the purpose of methods easier for other developers to understand.
- Avoids Accidental Overloading: Prevents creating a new method when overriding was intended.
Advantages of @Override Annotation
- Detects overriding errors at compile time.
- Prevents accidental method overloading.
- Improves code readability and understanding.
- Makes code maintenance easier.
- Clearly indicates that a method is overriding a parent or interface method