The second feature of object-oriented architecture:
Inheritance (extends ):
1. Inheritance Overview
1. Improved code reusability.
2. There is a relationship between the class and the class.
State features.
Note: do not inherit from other classes to simplify code;
A class can be inherited only when it has a link between the class and the class. Link is
.
2. inheritance features
3. Super keyword
4. Function coverage
5. Subclass instantiation Process
6. Final keywords
In Java: Java only supports single inheritance, but does not support multiple inheritance.
Why does it not support multi-inheritance?
Because multi-inheritance brings security risks: when multiple parent Classes define the same function,
When the function content is different, the subclass object is not sure which one to run.
C ++ can include multiple inheritance. Java optimizes multiple inheritance of C ++.
However, Java retains this mechanism and uses another embodiment to complete representation. Call
Multiple implementations.
JAVA supports multi-layer inheritance. That is, an inheritance system.
How to Use the functions in an inheritance system?
To use the system, first check the description of the system parent class, because the parent class defines
System features. By understanding the common features, you can know the basic features of the system.
Function.
This system can be basically used.
In actual use, we need to create the objects of the most subclass. Why?
1. Because it is possible that the parent class cannot create objects;
2. You can use more functions to create subclass objects, including basic and
Including unique.
One simple sentence: Read the parent class function and create a subclass object to use the function.
Aggregation: Who has whom (has)
Aggregation: the relationship between players and teams is called aggregation.
Combination: the hand is a part of the human body, closely linked
Inheritance:
After the child parent class appears: the characteristics of the class members.
Class member:
1. Variables
2. Functions
3. Constructor
1. Variables
If a non-private member variable with the same name appears in the subclass, The subclass needs to access
Variable. Use this; to access the variable with the same name as the parent class, use super.
The usage of super is almost the same as that of this.
This indicates the reference of the Class Object.
Super represents the reference of the parent class object.
2. Function
When the subclass has the same function as the parent class, when the subclass object calls this function,
It will run the content of the subclass function, just as the function of the parent class is overwritten.
This is another feature of the function: rewrite (overwrite ).
When a subclass inherits the parent class, it inherits the function of the parent class and is included in the subclass.
This function is available, but the content of the function is inconsistent with that of the parent class. This is unnecessary.
Define new features, but overwrite special features, retain the Function Definition of the parent class, and override
Function content.
Overwrite:
1. The subclass overwrites the parent class and must ensure that the permission of the subclass is greater than or equal to that of the parent class.
Otherwise, the compilation fails.
2. static data can overwrite static data.
Overload: only list of parameters of functions with the same name.
Rewrite: the sub-parent class methods must be identical.
(To be continued .. In fact, uploading these files is in the hope that you can take less steps and hope to help you !)