Saturday, November 3, 2012

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 coding
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 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.


I will give you one more example, where @Override annotation  has saved me a lot of times. Sometime I make mistake 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 same name without argument. This become really nasty, especially if original method is not abstract, because then compiler will not show any warning or error. But if you are using @Override annotation, Compiler will alert you with 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 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.

You should always use @Override annotation whenever application, suggested by Google's Java best practice guide as well. @Override is legal in following cases :

  1. When a class method is overriding a super-class method.
  2. When a class method is implementing an interface method.
  3. When an interface method respecifying a super-interface method.

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

Other Java best practices tutorial from Javarevisited Blog
20 design pattern interview questions for Java developers

2 comments :

Anthony Goubard said...

And please remove all the
/*
* (non-javadoc) Overrides ...
*/
comments.

manju said...

Good article Javin. This is very helpful if we are not using IDEs

Post a Comment