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 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");    }}Â
// Class 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 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 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


… [Trackback]
[…] Read More Information here to that Topic: geeksforgeeks.org/the-override-annotation-in-java/ […]