Inherit :
Extract multiple classes into an independent class , so that independent classes and multiple classes have an inheritance relationship
Inherited keyword : extends
Format :
class child class name extends parent class name {
...
}
Benefits of inheritance :
1) Improve the reusability of the code and solve the problem of bloated code ;
2) It is a premise of polymorphism . (The premise of polymorphism is that there must be an inheritance relationship)
Inherited features :
The subclass inherits the parent class, which inherits all the things of the parent class (member variables, member methods, including private methods), but the subclass cannot use private things, and can only indirectly allow the subclass to access through the public access of the parent class it .
Another feature of inheritance :
In Java, inheritance only supports single inheritance , not multiple inheritance (subclass name extends parent class name 1, parent class name 2,...)
However , Java can support multiple layers of inheritance .
The relationship between classes and classes : inheritance relationship
The relationship between class and interface : implementation relationship
It can be understood like this: (multi-layer inheritance) (the following member methods are public by default)
There is a GrandFather class whose member method is function() ;
There is Monther class, whose member method is method() ;
Create a new Father class to inherit ( extends ) the GrandFather class, which also inherits the member method function() of the GrandFather class , and the Father class also has its own member method show() .
Create a new Son class to inherit ( extends ) the Father class, which also inherits the member methods function() and show() of the Father class, and the Son class also has its own member method playGame() .
Therefore, it can be seen during the test: The Son class has a total of three member methods, two of which are inherited. Son s = new Son();
s.function();
s.show();
s.playGame();
Notes on inheritance :
1) Constructors cannot be inherited, but can be accessed through the super keyword;
2) Private can be accessed indirectly;
3) When to use extends?
Assumption : There is a class A
class A{
public void show1(){}
public void show2(){}
}
There is a class B
class B{
public void show1(){}
public void method(){}
}
According to the normal situation : it is found that class A and class B have a common show1() method, according to the concept of inheritance, ----> let class B inherit class A
class B extends A{
public void method(){}
}
This is no problem , but inheriting class A, show1(), show2() is also inherited, maybe show2() is not the function I want; don't use inheritance for this case
Inheritance embodies an "is a" relationship:
If A is a kind of B or B is a kind of A, this can use inheritance!
Fruit ----------Banana-Apple-Orange (all three belong to fruit)
Don't arbitrarily use inheritance , use it whenever there is an "is a" relationship.
Java development design principles : low coupling , high cohesion
Coupling : The relationship between classes and classes to minimize coupling.
Cohesion : refers to the ability to do one thing (try to use one class to complete things, not multiple classes to complete..)
In inheritance , the name problem of member variables
When the current subclass inherits the parent class , when the member variable names in the subclass and the parent class are inconsistent, they can be output separately;
When the member variable names in the subclass and the superclass are the same :
1) First go to the local location of the subclass to find, if found, output,
2) If it is not found, go to the member position of the subclass to find it, and output it if there is.
3) If the member position of the class has not been found, look for the member position of the parent class directly, and output if there is
4) If not, it will report an error, this variable does not exist.
Question :
To access variables in the local location of this subclass , you can access directly.
Requirement : To access the variable nun of the member location of the Son class, how to access it?
Requirement : To access the variable nun of the member location of the Father class. How to access?
Java provides the keyword: super: the space identifier of the parent class (reference of the parent class or object of the parent class)
Usage of this and super keywords:
member variable :
this:
this. member variable; (access to the current class)
super:
super. member variable; (access parent class)
Constructor :
this();//No parameter construction
this("");//Access the parameterized construction of the current class
super()://The no-argument construction of the parent class accessed
super(""):// accesses the parameterized construction of the parent class
Member method : this.xx()
super.xx()
E.g
System.out.println(num);//Current class local position
System.out.println(this.num); //this represents the current class object: Son
System.out.println(super.num); //The parent class reference represented by super
Questions about inherited members
Constructor :
The subclass inherits the parent class , and will access the no-argument constructor of the parent class by default.
This is because: if the data has not been initialized , then the parent class should be initialized first, and then the subclass should be initialized ---> hierarchical initialization
Think :
What if the no-argument constructor of the parent class is not provided ?
First of all, it must report an error ;
How to fix :
1) The parameterless structure of the parent class can be provided,
2) You can use the super keyword to access the parameterized structure of the parent class,
3) You can also access the parameterized structure in this class through this() in the subclass, and indirectly access parent class.
The constructor of the subclass must have one (construction with parameters/construction without parameters), let the parent class initialize it!
Problems with member methods in inheritance
The subclass inherits the parent class , and if the access member method names are inconsistent, call them separately!
When the member method name in the subclass is the same as the member method name in the parent class :
1) Now find the member location of the subclass, and call it if there is one;
2) If it is not found, find it in the member position of the parent class, and call it if there is.
About the usage of inheritance :
The parent class that is modified by private can be inherited, but can only access private indirectly;
The member properties of the parent class modified by private cannot be directly accessed by the subclass;
The subclass cannot inherit the constructor of the superclass , but can access it through super;
The subclass inherits the parent class . The parent class is initialized first, and then the subclass is initialized. This is the hierarchical initialization in inheritance.
Class name object name = new class name () ; In this sentence, the object is created and initialized by the constructor (default initialization, display initialization)