Java inheritance overview, java inheritance Overview
Benefits of inheritance:
1. improves code reusability.
2. The relationship between the class and the class provides a prerequisite for the third feature polymorphism.
Java supports single inheritance and does not directly support multi-inheritance. Because multiple parent classes have the same members, the call uncertainty is poor. However, the multi-Inheritance Mechanism in C ++ is improved.
Single inheritance: a subclass can only have one direct parent class.
Multi-inheritance: a subclass can have multiple direct parent classes (which are not allowed in java and can be improved through "Multi-Implementation" in Java)
Java supports multi-layer (Multi-Level) inheritance, and the inheritance system will appear.
C inherits B, B inherits
To use an inheritance system
1. view the top-level classes in the system to understand the basic functions of the system.
2. Create the most subclass object in the system and use the function.
When to define inheritance. Affiliation: is a relation
When there is a relationship between the class and the class, the inheritance is defined. XXX is one of YYY, XXX extends YYY
The specific embodiment of a member in the Child parent class.
1. member variables
This is used to distinguish between the member variables of this class and the local variables with the same name.
When the name of a member variable in the Child parent class is distinguished by super.
This is similar to super in usage.
This indicates the reference of a Class Object.
Super: represents the space of a parent class.
2. member functions
When the sub-parent class has identical member functions, the sub-class function is run.
This phenomenon is called overwrite operations. This is a feature of the function in the Child parent class.
Two features of a function:
1) overload, in the same class. Overload
2) overwrite. Subclass. Overwrite is also an overwrite. Override
Precautions for overwriting.
1) when the subclass method overwrites the parent class method, the permission of the subclass must be greater than or equal to the permission of the parent class.
2) static data can only be overwritten or overwritten.
When do I use overwriting?
When extending a class, the subclass must retain the function declaration of the parent class,
However, to define the special content of this function in the subclass, it is done by overwriting.
3. Constructor
When a subclass constructs an object, it is found that the parent class also runs when accessing the subclass constructor.
Cause: There is a default implicit statement in the first line of the subclass constructor. Super ();
Subclass instantiation process: All constructors in the subclass access the constructors with null parameters in the parent class by default. (Only access the constructor of the parent class once)
Why access the constructor in the parent class during subclass instantiation?
This is because the subclass inherits the parent class and obtains the content (attributes) of the parent class. Therefore, before using the content of the parent class,
First, you need to check how the parent class initializes its own content. To complete this required action, super () is added to the constructor of the subclass ().
If no null parameter constructor is defined in the parent class, the constructor of the subclass must use super to explicitly call the constructor in the parent class. At the same time, if the subclass constructor uses this to call the constructor of this class, super will be absent, because both super and this can only be defined in the first line. So there can only be one, but it can be guaranteed that there will certainly be other constructors in the subclass to access the constructors of the parent class.
Note: The super statement must be defined in the first line of the subclass constructor. Because the initialization of the parent class must be completed first.
Class Demo
{
Demo ()
{
Super (); // Object class, inherited by default extend
Return;
}
}
Disadvantages of inheritance: it breaks the encapsulation.