Home  >  Article  >  Java  >  What is the importance of @Override annotation in Java?

What is the importance of @Override annotation in Java?

王林
王林forward
2023-08-27 10:41:061401browse

What is the importance of @Override annotation in Java?

@Override annotation is one of the default Java annotations and can be introduced in Java 1.5 version. The @Override annotation indicates that the subclass method is overriding its base class method .

@Override Annotations work for two reasons

  • It will pull a warning from the compiler if the annotated method doesn't actually override anything.
  • can improve the readability of source code.

Syntax

public @interface Override

Example

class BaseClass {
   public void display() {
      System.out.println("In the base class,test() method");
   }
}
class ChildClass extends BaseClass {
<strong>   </strong>@Override <strong>
</strong>   public void display() {
      System.out.println("In the child class, test() method");
   }
}
// main class<strong>
</strong>public class OverrideAnnotationTest {
   public static void main(String args[]) {
      System.out.println("@Override Example");
      BaseClass test = new ChildClass();
      test.display();
   }
}

Output

@Override Example
In the child class, test() method

The above is the detailed content of What is the importance of @Override annotation in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete