classa{ Public voidShow () {Show2 (); } Public voidShow2 () {System.out.println (I); }}classBextendsA {/*Public Void Show () {Show2 (); } */ Public voidShow2 () {System.out.println (Love); }}classCextendsb{ Public voidShow () {Super. Show (); } Public voidShow2 () {System.out.println (You); }} Public classduotaitest4{ Public Static voidMain (String[]args) {A A=NewB (); A.show (); b b=NewC (); B.show (); }}
Summarize:
Features of polymorphic member access:
Method: Compile to see left, run to see right.
When inheriting:
Subclasses have the same method as in the parent class, called Overrides.
There are no methods in the subclass that have occurred in the parent class, and the method is inherited.
A.show ();
At compile time, look to the left, A has the show () method, compile without error, run to run the Show method in B.
(b Although did not write the show method, but in fact, the show in A has inherited!)! )
B.show ();
Run look to the right:
public void Show () { Super. Show (); }
and Super calls the Show method in the parent class B, and B in Show calls Show2 (), although B also has show2 (), but don't forget, we actually create the object of C, (new C ();), the last run to see the right, Don't forget it.
public void Show () { show2 (); }
Above.
Java polymorphic---ABC case list