First, one mechanism for a JVM implementation reference is that a reference to a class instance is a pointer to a handle (handle), which is a bunch of pointers: a pointer to a piece of memory space allocated from the Java heap, and another pointer to a table ( In fact, this table also has two pointers (one pointer to a method table containing the object, and another to the class object, indicating the type to which the object belongs))
The following examples are analyzed: Student st=new Student ();//student as subclass
Person P=st;//person for Super class
P.sayhello ();//The method exists in both superclass and subclass
Analysis: * Why object instances of a subclass's type can be covered by a superclass reference? Auto-enable up-shift. With this statement, the compiler automatically moves the subclass instance up to become a generic type person; *p.sayhello () will execute the subclass or the method defined by the parent class? Sub-class. At run time, the corresponding method is obtained by referencing the actual type according to the P object. So there is polymorphism. A superclass object reference, which is given a different subclass object reference, behaves differently when the method is executed. It is important to note that there are still two handles, p and St, in P=st, but P and St have the same chunk of data memory and different function tables. Dynamic linking: When a method in a parent class is defined in a parent class and not overridden in a subclass, it can be called by a reference to the parent class type, and for a method defined in the parent class, if the method is overridden in a subclass, a reference to the parent class type invokes this method in the subclass, which is the dynamic join. Summarize it for:
One, using a super-type reference to the sub-class object;
The reference can only invoke methods and variables defined in the superclass;
Thirdly, if a method in a superclass is overridden in a subclass, the method in the subclass is called when the method is called; (dynamic connection, dynamic invocation)
Variables cannot be overridden (overwritten), the concept of "overriding" is only for methods, and if a variable in the base class is "overridden" in a subclass, an error is made at compile time. A dynamic method call is implemented by assigning a subclass object reference to a superclass object reference variable. This mechanism of Java follows a principle: When a Superclass object references a variable that references a subclass object, the type of the referenced object rather than the type of the reference variable determines which member method to call, but the method that is called must be defined in the superclass, that is, the method covered by the quilt class. 1. If a is a reference to Class A, then a can point to an instance of Class A, or to a subclass of Class A. 2. If a is a reference to interface A, then a must point to an instance of a class that implements interface A.
The law of subclass object referenced by Java superclass