What is @Override annotation and when to use it in Java

October 13, 2016

@Override Annotation tells the compiler that this method overrides a superclass method.
This annotation is optional but is good practice to use it whenever you override a method.

In above example, we override print method and use @Override Annotation

Why should you use @Override Annotation?

When you use this annotation compiler will generate error when

1. You misspell method name, parameter name, parameter type.

Without @Override when you misspell name/parameter, subclass method would behave as a new method.

2. You change the signature of overridden method, and you can quickly find all class that requires the change of name.

 

Click here to add a comment

Leave a comment: