Inheritance: (extends)
Many classes, but some members, are duplicates, so to provide reusability, to extract the duplicated code, apply the inheritance
1. Improved reusability of the code
2. Let the class and the class have a relationship, inherit to the polymorphism provides the premise, no inheritance is not polymorphic
Concept Understanding diagram:
Single inheritance and multiple inheritance
Java supports single inheritance and does not directly support multiple inheritance, but it is improved on the multiple inheritance mechanism of C + +.
Defined:
1, single inheritance: A subclass can have only one direct parent class.
Class A extends B, b extends C;
2, multiple inheritance: A subclass can have multiple direct parent classes, (not allowed in Java) has been improved.
Class A extends B; A extend C;
Advantages of multiple Inheritance:
There are some variables in class A, there are some things in class B, if C inherits A and B, then C has a B
Disadvantages of C + + multiple inheritance:
See Code:
#include <iostream>using namespace Std;class a{public:void Show () {cout<< "SD" <<ENDL;} Class B{public:void Show () {cout<< "SSSD" <<endl;}} Class C:p ublic a,public b//a,b Show is the same function name {}c Blf;int main () {blf.show ();//The Multiple inheritance of C + + code here, there will be confusion, you must let Show,show1, Show2, Name guarantee is not the same, can return 0;}
Java Multi-Inheritance: does not directly support multiple inheritance, because once the same member in the parent class, there will be uncertainty, Java improved the C + + this flaw
Java's multi-inheritance is achieved through the "multi-implementation" approach.
Java supports multilayer (multiple) Inheritance: D Inherits C,c Inheritance B,b inherits a, the inheritance system appears,
Note : When you want to use an inheritance system:
1. Review the top level of the system to understand the basic functions of the system
2. Create the most sub-class object of the system, complete the use of the function
Inheritance system diagram:
Not finished ...
Java Learning Lesson 11th (inheritance)