Tag Archives: Annotation in java

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:

Click here to read more