1. Read the following section.Code
Class parentclass {Public String MSG = "I Am a attribute in parentclass. "; Public String getmsg () {return MSG;} class childrenclass extends parentclass {Public String MSG =" I Am a attribute in childrenclass. ";}public class testclass {public static void main (string [] ARGs) {parentclass P = new parentclass (); childrenclass c = new childrenclass (); system. out. println (P. getmsg (); system. out. PRI Ntln (""); system. Out. println (C. getmsg (); // The method in the parent class is not overwritten or called. }}
The output result is:
I am a attribute in parentclass.
I am a attribute in parentclass.
When the subclass does not override the method in the parent class, it calls the method in the parent class. when calling the method, it always finds out whether there is a subclass extension method, if not, go to the parent class. Here, the subclass does not have a method to extend the parent class.
2. dynamic binding
When a subclass object is created (new subclass (), the reference of the parent class is given on the left, that is, the parent class reference name = new subclass ()
The system dynamically binds the actual type of reference.