JAVA 11th (inheritance)
Inheritance: (extends)
Many classes, but some members are repeated. To provide reusability, duplicate code is extracted, and inheritance is applied.
1. Improved code reusability
2. Relationship between classes,Inheritance provides the premise for polymorphism, and there is no polymorphism without inheritance.
Class MAN {String name; int age;} class student extends MAN // MAN is the base class of student, and student is the subclass of MAN {void study () {System. out. println (name + "Learning" + age);} class teacher extends MAN {void work () {System. out. println (name + "working" + age) ;}} public class Main {public static void main (String [] args) {student BLF = new student (); BLF. name = "BLF2"; BLF. age = 20; BLF. study ();}}
Concept diagram:
Single inheritance and multi-Inheritance
JAVA supports single inheritance and does not directly support multi-inheritance. However, it is improved on the Multi-Inheritance Mechanism of C ++.
Definition: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Upload + upload/upload + PGJyPgo8L3A + CjxwPjxzdHJvbmc + PC9zdHJvbmc + PHByZSBjbGFzcz0 = "brush: java;"> # include Using namespace std; class A {public: void show () {cout <"sd" <
Multi-inheritance in java: Multi-inheritance is not directly supported, because once the same members exist in the parent class, uncertainty will occur. java improves the C ++ defect.
Java multi-inheritance is implemented through "Multi-Implementation ".
Java supports multi-layer (Multi-Level) Inheritance: D inherits C, C inherits B, and B inherits A, so the inheritance system appears,
Note:: To use an inheritance system:
1. view the top level of the system and understand the basic functions of the system.
2. Create the most subclass object of the system and use the function
Inheritance System diagram:
Unfinished ......