Skip to main content

Inheritence problem(a demo form Thing in java)

Please note these forums are being decommissioned and use the new and improved forums at
1 reply [Last post]
Joined: 2011-02-17

while reading Thinking in java, I get confused when I saw the example below(the example is in page 290, Chapter polymorphism):

public class PrivateOverride{<br />
private void f(){print(&quot;private f()&quot;);}<br />
public static void main(){<br />
PrivateOverride po = new Derived();<br />
po.f();<br />
}<br />
}<br />
class Derived extends PrivateOverride{<br />
public void f(){print(&quot;public f()&quot;)}<br />

the result is private f();
I have two confusions:
1: the author said that when we inherit, we automatically get all the fields and mehtods in the base class(page 241 Inheritence syntax),so in the case above, does class Derived has a private mehtod called f()?
2: what underlying mechanism makes it possible for a derived class reference to invoke a method defined in the base class?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2011-10-07


In the above example we are not allow to inherit the private method f(). Inheritance does not allow the derived class to inherit the private members of the base class althrough you can inherit the public as well as protected members of the base class.

To invoke a method defined in the base class we use super keyword. The super keyword allows you to call the base class method from the derived class. Its syntax :