Friday, October 3, 2014

Why to use @Override Annotation in Java

If you are new to annotation in Java, then this article will give an exciting example to understand what Annotation actually means in Java. We'll also understand what is Annotation and why we need them with one example of @Override Annotation.

Annotations were introduced in Java 5. Before I define annotations and discuss their importance in programming, let’s discuss a simple example. 

Suppose you have an Employee class, which has a method called setSalary() that sets the salary of an employee. The method accepts a parameter of the type double. The following snippet of code shows a trivial implementation for the Employee class:

A Manager class inherits from the Employee class. You want to set the salary for managers differently. You decide to override the setSalary() method in the Manager class. The code for the Manager class is as follows

Note that there is a mistake in the above code for the Manager class, when you attempt to override the setSalary() method. (You’ll correct the mistake shortly.) You have used the int data type as the parameter type for the incorrectly overridden method. It is time to set the salary for a manager. The following code is used to accomplish this:


This snippet of code was expected to call the setSalary() method of the Manager class but the output does not show the expected result.

What went wrong in your code? The intention of defining the setSalary() method in the Manager class was to override the setSalary() method of the Employee class, not to overload it. You made a mistake. You used the type int as the parameter type in the setSalary() method, instead of the type double, in the Manager class

You put comments indicating your intention to override the method in the Manager class. However, comments do not stop you from making logical mistakes. You might spend, as every programmer does, hours and hours debugging errors resulting from this kind of logical mistake.

Who can help you in such situations? Annotations might help you in a few situations like this.
Let’s rewrite your Manager class using an annotation. You do not need to know anything about annotations at this point. All you are going to do is add one word to your program. The following code is the modified version of the Manager class:
All you have added is a @Override annotation to the Manager class and removed the “dumb” comments. Trying to compile the revised Manager class results in a compile-time error that points to the use of the @Override annotation for the setSalary() method of the Manager class: error: method does not override or implement a method from a supertype @Override

The use of the @Override annotation did the trick

  • The @Override annotation is used with a non-static method to indicate the programmer’s intention to override the method in the superclass. 

At source code level, it serves the purpose of documentation. When the compiler comes across the @Override annotation, it makes sure that the method really overrides the method in the superclass. If the method annotated does not override a method in the superclass, the compiler generates an error.

In your case, the setSalary(int salary) method in the Manager class does not override any method in the superclass Employee. This is the reason that you got the error. You may realize that using an annotation is as simple as documenting the source code. However, they have compiler support. You can use them to instruct the compiler to enforce some rules. Annotations provide benefits much more than you have seen in this example.

You can fix the error by doing one of the following two things:

  • You can remove the @Override annotation from the setSalary(int salary) method in the Manager class. It will make the method an overloaded method, not a method that overrides its superclass method.
  • You can change the method signature from setSalary(int salary) to setSalary(double salary). Since you want to override the setSalary() method in the Manager class

Note that the @Override annotation in the setSalary() method of the Manager class saves you debugging time.

What is Annotation
It lets you associate (or annotate) metadata (or notes) to the program elements in a Java program. The program elements may be a package, a class, an interface, a field of a class, a local variable, a method, a parameter of a method, an enum, an annotation, a type parameter in a generic type/method declaration, a type use, etc. In other words, you can annotate any declaration or type use in a Java program. An annotation is used as a modifier in a declaration of a program element like any other modifiers (public, private, final, static, etc.). Unlike a modifier, an annotation does not modify the meaning of the program elements. It acts like a decoration or a note for the program element that it annotates.

An annotation differs from regular documentation in many ways

  • A regular documentation is only for humans to read and it is “dumb.” 
  • An annotation serves this purpose. It is human readable, which serves as documentation. It is compiler readable, which lets the compiler verify the intention of the programmer; for example, the compiler makes sure that the programmer has really overridden the method if it comes across a @Override annotation for a method. Annotations are also available at runtime so that a program can read and use it for any purpose it wants.
For example, a tool can read annotations and generate boilerplate code. If you have worked with Enterprise JavaBeans (EJB), you know the pain of keeping all the interfaces and classes in sync and adding entries to XML configuration files. EJB 3.0 uses annotations to generate the boilerplate code, which makes EJB development painless for programmers.
Another example of an annotation being used in a framework/tool is JUnit version 4.0. JUnit is a unit test framework for Java programs. It uses annotations to mark methods that are test cases. Before that, you had to follow a naming convention for the test case methods. Annotations have a variety of uses, which are documentation, verification, and enforcement by the compiler, the runtime validation, code generation by frameworks/tools, etc.

An annotation does not change the semantics (or meaning) of the program element that it annotates. In that sense, an annotation is like a comment, which does not affect the way the annotated program element works. For example, the @Override annotation for the setSalary() method did not change the way the method works.

If you know anyone who has started learning java, why not help them out! Just share this post with them. Thanks for studying today!...


  1. Nice post, really its worthy....

  2. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training from India . Nowadays Java has tons of job opportunities on various vertical industry.