InterviewSolution
Saved Bookmarks
| 1. |
May I override a public method with a protected method? Why is it so? |
|
Answer» We cannot override a public method by a protected method. The ACCESS modifiers of the method in the child class cannot limit the scope of the method of the parent class while overriding. This is because we call the method through a SUPERCLASS reference which overrides the parent implementation by the child implementation. As the type of reference is of Parent class, the client code knows the API with the broader scope (public) and is prepared BASED on the parent API. So, it doesn’t make any sense to limit the scope in the overriding (child) method, but the opposite is possible, of course. |
|