Sunday, 26 January 2014

Why use @Override annotation in Java - Coding Best Practice

@Override annotation was added in JDK 1.5 and it is used to instruct 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 which accept Object as parameter just like equals, compareTo or compare() method of Comparator 
interface. @Override is one of the three built in annotation provided by Java 1.5, 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 than you have see that one of the mistake Java programmer makes it,  write equals method with non object argument type as shown in below example:
public class Person{
    private String name;
 
    public boolean equals(Person person){
        return name.equals(person.name);
    }
}

@Override annotation in java best practice codinghere 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 prevent 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 lot of debugging effort by avoiding this severe mistake in Java. This single reason is enough to convince programmer to always use @Override annotation while implementing super type methods.

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 1.6

One of the major problem with @Override annotation on JDK 1.5 was that it can only be used to in conjunction with super class i.e. compiler throws error if you use @Override annotation with interface method. From Java 6 onwards you can use @Override annotation while implementing interface method as well. This provides robust compile time checking of overriding. If you have been using Eclipse IDE than you must have faced issue along @Override annotation where compiler complains even if you override interface method and only fix was either remove all @Override annotation from interface method or shift to Java source 1.6 from compiler settings.

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 super class or interface.

Other Java best practices tutorial from Learn About Linux Blog
20 design pattern interview questions for Java developers

No comments:

Post a Comment