Wednesday, April 27, 2011

Override Annotation

Note that it is the most used annotation...
Override is a marker annotation type that can be applied to a method to indicate to the compiler that the method overrides a method in a superclass. This annotation type guards the programmer against making a mistake when overriding a method. For example, consider this class Parent:
class Parent {
    public float calculate (float a, float b) {
        return a * b;
    }
}
Whenever you want to override a method, declare the Override annotation type 

before the method:

public class Child extends Parent {
    @Override
    public int calculate (int a, int b) {
        return (a + 1* b;
    }
}

No comments:

Post a Comment

Chitika