
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Importance of @Override annotation in Java?
What is an Annotation?
An annotation is like metadata that provides additional information about the code. It does not affect the code directly but provides the information so that the compiler or runtime environment can use it while executing the code.
To define or use any annotation in Java, you need to specify the annotation name starting with the "@" symbol. Here is the syntax to use annotations in Java:
@annotation_name
Where the @ symbol specifies the annotation, and annotation_name is the name of the annotation. Following is a list of a few important annotations and their usage:
Sr.No | Annotation | Usage |
---|---|---|
1. | @Override | It is used to ensure that a method is correctly overriding a method from its superclass (or parent class). |
2. | @Deprecated | It is used to mark a method, class, or field as obsolete or deprecated, which indicates that it should no longer be used. |
3. | @SuppressWarnings | It instructs the compiler to hide specific compile-time warnings. |
@Override Annotation in Java
In Java, the @Override annotation is one of the default Java annotations, and it was introduced in Java 1.5. The @Override annotation indicates that the child class (or derived class) method overrides its base class (parent class) method.
In addition, the @Override annotation informs the compiler that the element is meant to override an element or method declared in a superclass. If a method is marked with the @Override annotation and fails to override the method correctly, then the compiler will generate an error.
Syntax
Following is the syntax of the @Override annotation:
@Override method_name() { // method body }
Where the method_name is the method of your base class that you want to override in the child class.
Example
In the following example, the child class overrides its parent (super) class method using the @Override annotation:
// Parent class class BaseClass { public void display() { System.out.println("In the base class, display() method"); } } // Child class extends parent using inheritance class ChildClass extends BaseClass { // Using @Override annotation to override the parent method @Override public void display() { System.out.println("In the child class, display() method"); } } public class OverrideAnnotationTest { public static void main(String[] args) { System.out.println("Java @Override Annotation Example"); // Reference of parent class pointing to object of child class BaseClass test = new ChildClass(); // Calling the overridden method in ChildClass test.display(); } }
The above program produces the following output:
Java @Override Annotation Example In the child class, display() method
Importance of the @Override Annotation
Here are a few important points of the @override annotation in Java:
- It is used to override the parent method.
- It detects an error at compile time if the annotated method doesn't override anything or incorrectly override the method.
- It improved the code readability so that anyone can understand the method override from its parent class.