Thursday 13 September 2018

What exactly does @Override do in Java?

       @Override is annotation which is used to make compiler check all rules of overriding.If we don't write @Override over the method, then compiler will apply all overriding rules only if below three point are :
1. There are super class and sub class relation.
2. Method names are same.
3. Parameters are same.
But if we have written as @Override on sub class method, then compiler must apply all rules irrespective of above three points.
Below are the examples which will explain the use of @Override annotation.
1. Example without @Override annotation.























In the above example, there is no compile time and run time error. We will get output.
Child is called.

2. Example with @Override annotation.
























In the above example, there is compile time error when we use @Override annotation that is we can not override private method.

No comments:

Post a Comment