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 do
Concrete1 c1 = new Concrete1(); c1.show( );
Now the output is displayed
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) An object c1 of a specific object is created
3) c1.show() . Since P1.Base.show () is public , it can be called.
4) Now in P2.concrete1, after inheritance, only native methods (pri, pac, pro, pub) and inherited methods P1.Base (pro, pub, show) are available.
Now WHY does it show Base.pri () and Base.pac () in the output when they are not even available?
It is clear that I do not have a clear fundamental understanding of inheritance. Can anyone explain this situation and how inheritance is really “structured”. I used to think that inherited methods and fields of a superclass simply overlap a subclass. But this line of reasoning is obviously wrong.
Thanks!
user1265125
source share