@Override annotation is used when we override a method in sub class. Generally novice developers overlook this feature as it is not mandatory to use this annotation while overriding the method. Here we will discuss why we should use @Override annotation and why it is considered as a best practice in java coding.
Lets take an example first to understand how it is used then we will discuss it in detail:
Example
class ParentClass { public void displayMethod(String msg){ System.out.println(msg); } } class SubClass extends ParentClass { @Override public void displayMethod(String msg){ System.out.println("Message is: "+ msg); } public static void main(String args[]){ SubClass obj = new SubClass(); obj.displayMethod("Hey!!"); } }
In the above example we are overriding a method displaymethod()
in the child class. Even if we don’t use the @Override annotation, the program would still run fine without any issues, You would be wondering the why do we use this annotation at all. Lets discuss about it:
Why we use @Override annotation
Using @Override annotation while overriding a method is considered as a best practice for coding in java because of the following two advantages:
1) If programmer makes any mistake such as wrong method name, wrong parameter types while overriding, you would get a compile time error. As by using this annotation you instruct compiler that you are overriding this method. If you don’t use the annotation then the sub class method would behave as a new method (not the overriding method) in sub class.
2) It improves the readability of the code. So if you change the signature of overridden method then all the sub classes that overrides the particular method would throw a compilation error, which would eventually help you to change the signature in the sub classes. If you have lots of classes in your application then this annotation would really help you to identify the classes that require changes when you change the signature of a method.
Datt says
Hi Chaitanya, I would require a clarification from you with respect to the override annotation. You have above quoted as “If you don’t use the annotation then the sub class method would behave as a new method (not the overriding method) in sub class”. Because the subclass is already extending the parent class, ideally (without providing annotation) the method however will get overriden as per my understanding when it has implementation. Based on this, Can you please explain me the point sub-class method behaving as new method.
Raj says
Hi Datt, I guess Chaitanya meant : “When a programmer makes any mistake such as wrong method name, wrong parameter types while overriding and if annotation is not used, programmer won’t know as the compile time error won’t be thrown. And the sub class method would behave as a new method (not the overriding method) in sub class”
suyogya says
If you change displayMethod(String msg) to displayMethod(String msg, String msg2), program gives error of you have @override. If you comment @override program doesn’t give any error and prints as per displayMethod of SubClass.
Chaitanya Singh says
This is because when you change the signature to displayMethod(String msg, String msg2), you are not overriding the parent class method. @override annotation is used when you are overriding the method in the child class, this is why you get the error. When you remove the annotation, the displayMethod(String msg, String msg2) method is treated as a normal (not overriden) method of child class and you don’t get error. In short “If you use @override annotation on a method that you are not overriding, you will get an error”. For more information, refer this guide: Overriding in Java.