This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
When you are overriding the method from parent class, its not called "accessing" - you are basically modifying the functionality of the base class - completely changing it or enhancing it.
Accessing a private function of the super class would still give compile time error. narrowing down the privilege of the overridden method in child class will also give you compile time error.
jacob deiter wrote:In the derived class I can override private method.
No you can't - in order to override a method, that method must be visible in the overriding class. Therefore, private methods can never be overridden.
Instead, you are simply implementing a new method that happens to have the same signature (name + parameters + return type).
narrowing down the privilege of the overridden method in child class will also give you compile time error.
can you give some example?
in your reply, you says that private method of base class can be overridden in sub class.
if it so then protected method of base class can not be overridden in sub class ??
What amitabh meant is the following:
- a public method must be public in any sub class.
- a protected method must be protected or public in any sub class
- a method with default access must have default access or be protected or public in any sub class
In other words, you can't reduce the visibility.
This is not really related to your initial question though.