@Override annotation was added in JDK 1.5 and it is used to instruct the compiler that method annotated with @Override is an overridden
method from super class
or interface.
Though it may look trivial @Override is particularly useful while overriding methods that accept Object as a parameter just like equals,
compareTo
or compare() method of Comparator interface. @Override is one of the three built-in annotations provided by
Java 1.5, the other two are @SuppressWarnings and @Deprecated.
Out of these three @Override is most used because of its
general nature, while @SuppressWarnings is also used while using Generics, @Deprecated is
mostly for API and library.
If you have read my article common errors while overriding equals method then you have seen that one of the mistake Java programmer makes it, write equals method with non-object argument type as shown in the below example:
If you have read my article common errors while overriding equals method then you have seen that one of the mistake Java programmer makes it, write equals method with non-object argument type as shown in the below example:
public class Person{
private String name;
public boolean equals(Person person){
return name.equals(person.name);
}
}
private String name;
public boolean equals(Person person){
return name.equals(person.name);
}
}
here programmer is attempting to override equals() method, but instead of overriding, its a overloaded method. This error silently escape from compiler and not even surface on runtime by any Exception and it’s very hard to detect. @Override annotation in Java prevents this category of mistake.
If you put @Override annotation above equals method than compiler will verify if this method actually overrides a super class or interface method or not. if its not then it throw compilation error like "method does not override or implement a method from a super type.
In short @Override annotation saves a lot of debugging effort by avoiding this severe mistake in Java. This single reason is enough to convince programmers to always use @Override annotation while implementing super type methods.
I will give you one more example, where @Override annotation has saved me a lot of times. Sometimes I make mistakes like, overriding method without argument, which means, I intend to override a method, which takes an argument and ends up writing a new method with the same name without argument.
This becomes really nasty, especially if the original method is not abstract, because then the compiler will not show any warning or error. But if you are using @Override annotation, the Compiler will alert you with an error that it actually does not override super class method.
Apart from compile-time checking of overriding, @Override
can also be used by IDE and static code analyzer to suggest a default
implementation or coding
best practices.
@Override annotation in Java with Example
One of the major problems with @Override annotation
on JDK 1.5 was that it can only be used in conjunction with super class
i.e. compiler throws an error if you use @Override annotation
with interface
method.
From Java 6 onwards you can use @Override annotation while implementing the interface method as well. This provides robust compile-time checking of overriding. If you have been using Eclipse IDE then you must have faced issue along with @Override annotation where compiler complains even if you override interface method and only fix was either remove all @Override annotation from the interface method or shift to Java source 1.6 from compiler settings.
You should always use @Override annotation whenever application, this is even suggested by Google's Java best practice guide as well. @Override is legal in the following cases :
The only place, where you don't want to use @Override is when the parent method is @Deprecated.
That's all on @Override annotation in Java. It's one of the best Java coding practice to use @Override annotation while overriding any method from superclass or interface. This will save you from making mistakes which are hard to find and debug with a live application.
From Java 6 onwards you can use @Override annotation while implementing the interface method as well. This provides robust compile-time checking of overriding. If you have been using Eclipse IDE then you must have faced issue along with @Override annotation where compiler complains even if you override interface method and only fix was either remove all @Override annotation from the interface method or shift to Java source 1.6 from compiler settings.
You should always use @Override annotation whenever application, this is even suggested by Google's Java best practice guide as well. @Override is legal in the following cases :
- When a class method is overriding a super-class method.
- When a class method is implementing an interface method.
- When an interface method re specifying a super-interface method.
The only place, where you don't want to use @Override is when the parent method is @Deprecated.
That's all on @Override annotation in Java. It's one of the best Java coding practice to use @Override annotation while overriding any method from superclass or interface. This will save you from making mistakes which are hard to find and debug with a live application.
This annotation has saved many mistakes during compile time from both junior and senior developers a like. Now days, IDE will automatically put the override annotation for you whenever you override a superclass method in Java.
Other Java best practices tutorial from Javarevisited Blog
20
design pattern interview questions for Java developers
And please remove all the
ReplyDelete/*
* (non-javadoc) Overrides ...
*/
comments.
Good article Javin. This is very helpful if we are not using IDEs
ReplyDelete