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

What is the importance of @Override annotation in Java?

王林
Release: 2023-08-27 10:41:06
forward
1493 people have browsed it

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
Copy after login

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();
   }
}
Copy after login

Output

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

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template