1. Object-oriented feature two: inheritance definition:
Refers to an object that uses properties and methods of another object directly.
Benefits:
1. Provide the reusability of the code.
2. The direct relationship between classes and classes provides a precondition for the third feature polymorphism.
Single inheritance is supported in Java. Multiple inheritance is not directly supported, but the multi-inheritance mechanism in C + + is improved.
Single inheritance: A subclass can have only one parent class.
Multiple inheritance: A subclass can have multiple direct parent classes (not allowed in Java, modified, not directly supported, because the same members in more than one parent class can produce call uncertainties, which are embodied in Java through a "multi-implementation" approach.
Java supports multilayer (multiple) inheritance. (for example, C inherits B,b inherits a, the inheritance system appears.) )
(Nonsense: When you want to use an inheritance system, 1 look at the top-level classes in that class to understand the basic functionality of the system.) 2 Create the most sub-class object in the system and complete the function usage. )
When do you define inheritance?
Inheritance is defined when the owning relationship exists between the class and the class. (XXX is a kind of yyy; XXX extends yyy)
When members of this class and local variables have the same name, this is distinguished by this.
When a member variable in a child parent class has the same name, the parent class is distinguished by super.
This and super are very similar in usage.
This: Represents a reference to an object of this class.
Super: Represents a parent class space.
2. Overwrite of function (override)
Concept: When a member function is identical in a child parent class, the function of the subclass is run.
This behavior, called the overwrite operation . This is the attribute of the function in the child parent class.
Function two properties:
1, overload. The same class. Overload
2, overlay. Sub-class. Overrides are also known as rewriting, overwriting. Override
Coverage considerations:
1, when a subclass method overrides a parent class method, the subclass permission must be greater than or equal to the permission of the parent class. (The parent class method is not called overwrite when it is private)
2, static can only override static, or be statically overwritten.
When do I use the overwrite operation?
When you extend a subclass to a class, the subclass needs to preserve the function declaration of the parent class, but when you want to define the unique content of the feature in the subclass, use the overwrite operation to complete.
Characteristics of constructors in child-parent classes
When the subclass constructs the object, it is discovered that when the subclass constructor is accessed, the parent class is also running.
Why is it?
The reason for this is that there is a default implicit statement in the first row of the constructor for the child class. Super ();
Instantiation of subclasses: all constructors in a subclass access the constructor of the null argument in the parent class by default.
Why do you want to access the constructor in the parent class when the subclass is instantiated?
That's because the subclass inherits the parent class and gets the contents (attributes) in the parent class, so before you use the parent class content, you first see how the parent class initializes its own content. So when a subclass constructs an object, it must access the constructor in the parent class. To complete this necessary action, the super () statement is added to the constructor of the subclass.
If the null argument constructor is not defined in the parent class, then the constructor of the subclass must use super to explicitly call which constructor in the parent class. At the same time, if the class constructor is called with this in the subclass constructor, then super is gone because super and this can only define the first row. So there's only one. However, it is guaranteed that other constructors in the subclass will have access to the constructor of the parent class.
Note: The SUPRE statement must be defined in the first row of the subclass constructor. Because the initialization of the parent class is done first.
An Object instantiation process:
For example, person p = new person ();
1,JVM reads the Person.class file under the specified path, loads it into memory, and loads the person's parent class first, if there is a direct parent class.
2. Open up space in heap memory and assign address.
3, and in object space, the default initialization of the properties in the object.
4, the corresponding constructor is called to initialize.
5, in the constructor, the first row is initialized by calling the constructor in the parent class first.
6, when the parent class is initialized, the properties of the child class are displayed for initialization.
7, and then perform a specific initialization of the subclass constructor.
8, the address value is assigned to the reference variable when the initialization is complete.
The picture is cracked (the steps are shown below):