Basic Concepts of Inheritance: (1) Java does not support multiple inheritance, which means that subclasses can have at most one parent class. (2) subclasses inherit member variables and member methods that are not private in their parent class, as their own member variables and methods. (3) The member variables defined in the subclass are the same as the member variables defined in the parent class, and the member variables in the parent class cannot be inherited. (4) A member method defined in a subclass, and the name return type of the method, and the number and type of the parameter are exactly the same as a member method of the parent class, the member methods of the parent class cannot be inherited. Analyzing the above program example, the main question is "subclass inherits the member variables of the parent class, will the parent object be instantiated (not only new will be instantiated)?" Will private member variables be inherited? Where is the inherited member variable allocated space? "1: The virtual machine loads the Extendsdemo class, extracting the type information into the method area. 2: By saving the bytecode in the method area, the virtual machine starts executing the main method, and the main method is in the stack. 3: Executes the first instruction of the main method, new Student (); The phrase is to allocate heap space to the student instance object. Because student inherits the person parent class, the virtual machine first loads the person class into the method area and initializes the parent class member variable in the subclass space in the heap. It then loads the student class into the method area, allocates space for the member variables of the student class, and initializes the default values. Assigns an instance object address of the student class to the reference variable S. 4: The next two statements assign a value to the member variable, and because name and age are inherited from the parent class, it is saved in the subclass object (See the heap in the diagram where the parent class member variable is allocated space in the subclass instance object and a reference to the parent class is saved, and the parent class is not instantiated. ), the object in the heap (the subclass object) is found based on the reference that the reference variable s holds, and then the name is assigned to age. 4: Call the Say () method, locate the instance object in the heap by referencing a reference to the variable s hold, locate the type information for this class, and navigate to the Say () method by referencing the class in the method area held by the instance object. Say () method into the stack. Begins execution of the bytecode in the Say () method. 5:say () method execution finished, say method out of the stack, the program back to the main method, the main method executes the stack, the main thread dies, the virtual machine instance dies, the program ends.
Summary: The same method is overridden, and the variable is not rewritten, if the subclass declares a variable that is the same as the parent class, it means that the subclass will have two variables of the same name. The private variable of the parent class is also inherited and initialized in the parent object of the subclass, except that it is not visible to the outside.
The Super keyword is used in Java to make a masked member variable or member method visible, or to refer to a masked member variable or member method, and super is simply a reference to the parent class characteristics (properties and methods) that are recorded inside the object. What is a masked member variable or member method? The quilt class overrides the method and defines the same member variable as the parent class, because it cannot be inherited, so it is called masked.
Speaking of which, the doubts raised above are untied.
"Go" Java Inherits memory allocations