VOOZH about

URL: https://www.geeksforgeeks.org/java/the-override-annotation-in-java/

⇱ The @Override Annotation in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

The @Override Annotation in Java

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


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


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
Comment
Article Tags: