Java Reference
In-Depth Information
Let's go back to the compile-time error. 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.
setSalary(int salary) to setSalary(double salary) .
Since you want to override the setSalary() method in the Manager class, use the second option and modify the
Manager class as follows:
You can change the method signature from
public class Manager extends Employee {
@Override
public void setSalary(double salary) {
System.out.println("Manager.setSalary():" + salary);
}
}
Now the following code will work as expected:
Employee ken = new Manager();
int salary = 200;
ken.setSalary(salary);
Manager.setSalary():200.0
Note that the @Override annotation in the setSalary() method of the Manager class saves you debugging time.
Suppose you change the method signature in the Employee class. If the changes in the Employee class make this
method no longer overridden in the Manager class, you will get the same error when you compile the Manager class
again. Are you starting to understand the power of annotations? With this background in mind, let's start digging deep
into annotations.
According to the Merriam Webster dictionary, the meaning of annotation is
“A note added by way of comment or explanation”.
This is exactly what an annotation is in Java. 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.” It has no intelligence associated with it. If you misspell a word, or state something in
the documentation and do just the opposite in the code, you are on your own. It is very difficult and impractical
to read the elements of documentation programmatically at runtime. Java lets you generate Javadocs from your
documentation and that's it for regular documentation. This does not mean that you do not need to document
your programs. You do need regular documentation. At the same time, you need a way to enforce your intent using
a documentation-like mechanism. Your documentation should be available to the compiler and the runtime. 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
 
Search WWH ::




Custom Search