The @Override Annotation in Java - GeeksforGeeks

06 Dec, 2021
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.
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 classesimport java.io.*; // Class 1// Parent classclass 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 classclass 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");    }} // Clas 3// OverrideAnnotationTestpublic 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 t ochild 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 classesimport java.io.*; // Class 1// Helper abstract classabstract class Vehicle {     // Calling this method    public abstract void method();} // Class 2// Helper classclass 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 classclass 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// OverrideAnnotationExamplepublic 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
singhhimanshu2009
anikakapoor
rkbhola5
Java-Object Oriented
Picked
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Exceptions in Java
Constructors in Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
How to Iterate HashMap in Java?
Iterate through List in Java