@Override Annotation
tells the compiler that this method overrides a superclass method.
This annotation is optional but is good practice to use it whenever you override a method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class Parent{ public void print(String msg){ System.out.println("parent say: " + msg); } } class Child extends Parent{ @Override public void print(String msg) { System.out.println("child say: " + msg); } } |
In above example, we override print method and use @Override Annotation
Why should you use @Override Annotation?
When you use this annotation compiler will generate error when
1. You misspell method name, parameter name, parameter type.
Without @Override when you misspell name/parameter, subclass method would behave as a new method.
2. You change the signature of overridden method, and you can quickly find all class that requires the change of name.