package P1;
public class Base {
private void pri( ) { System.out.println("Base.pri()"); }
void pac( ) { System.out.println("Base.pac()"); }
protected void pro( ) { System.out.println("Base.pro()"); }
public void pub( ) { System.out.println("Base.pub()"); }
public final void show( ) {
pri();
pac();
pro();
pub();
}
}
and
package P2;
import P1.Base;
public class Concrete1 extends Base {
public void pri( ) { System.out.println("Concrete1.pri()"); }
public void pac( ) { System.out.println("Concrete1.pac()"); }
public void pro( ) { System.out.println("Concrete1.pro()"); }
public void pub( ) { System.out.println("Concrete1.pub()"); }
}
And I’m executing
Concrete1 c1 = new Concrete1();
c1.show( );
Now, the output is shown to be
Base.pri()
Base.pac()
Concrete1.pro()
Concrete1.pub()
Can someone explain why this is so? From what I understood about inheritance, this should have happened:
1) P2.concrete1 inherits P1.Base
2) c1 object of concrete1 is created
3) c1.show() is called. Since P1.Base.show() is public, it can be called.
4) Now, in P2.concrete1 after inheritance, only it’s own methods (pri, pac, pro, pub) and P1.Base’s inheritable methods (pro, pub, show) are accessible.
Now WHY does it show Base.pri() and Base.pac() in the output when they’re not even accessible?
It’s clear that I’ve not got a very clear fundamental understanding of inheritance. Can someone explain this situation and how inheritance is actually ‘structured’. I used to think that inheritable methods and fields of the superclass would just superimpose onto the subclass. But that line of reasoning is obviously wrong.
Thanks!
The short answer is that you can only override methods that are visible. The first two methods,
priandpacare private and package protected respectively. Because nothing outside the class can see a private method, it can’t be overridden. Similarly becauseConcrete1is in a different package fromBaseit can’t seeBase.pacso cannot override it.What this means is that while you define a
priandpacmethod inConcrete1, they’re just methods that happen to have the same name as the methods inBase, not overrides. The other two methodsproandpubare protected and public respectively, so are visible toConcrete1. As a result theproandpubmethods inConcrete1are overrides of the methods of the same name inBase.Because
showis defined inBase, it’s compiled calling the 4 methods as defined inBase. When executed the JVM looks to see if any are overridden and if they are executes the overridden methods. As explained above,priandpacare not overridden so theBaseversions are executed where asproandpubare so theConcrete1versions are executed.If you were to move the
showmethod intoConcrete1instead ofBasethen it would execute the 4 methods as defined inConcrete1as those would be the methods visible toshow.