1. Inheriting extends (also called extension)
Introduced
First write two classes:
Defining student Classes
Class Student {
Member variables
private String name;
private int age;
Empty construction
Public Student () {}
GetXxx ()/setxxx ()
public void Eat () {
System.out.println ("eat");
}
}
Define teacher Classes
Class Teacher {
Member variables
private String name;
private int age;
Empty construction
Public Teacher () {}
GetXxx ()/setxxx ()
public void Eat () {
System.out.println ("eat");
}
}
2. Overview of Inheritance
When the same properties and behaviors exist in multiple classes, the content is extracted into a single class, so that multiple classes do not need to define these properties and behaviors, as long as they inherit that class.
Class and class inheritance can be implemented by the extends keyword, with the following format:
Class subclass name extends parent class name {}
This class alone, which is the inherited class, is called the parent class, the base class, or the superclass, and these classes are called subclasses or derived classes.
With inheritance, when we define a class, we can define our new members on the basis of a class that already exists.
3. Inherited cases and benefits of inheritance
Demonstrate code with a specific case
Case 1: Student class and teacher, define two functions (eat, sleep)
Case 2: Improving after joining the human race
/*
Inheritance Overview:
The same content in multiple classes is extracted to define a class.
Format:
Class subclass name extends parent class name {}
Benefits:
A: Improved reusability of the code
B: Improved maintainability of the code
C: The relationship between classes and classes is a prerequisite for polymorphism
*/
Before using inheritance
/*
Class Student {
public void Eat () {
System.out.println ("eat");
}
public void sleep () {
System.out.println ("Sleeping");
}
}
4. Benefits of Inheritance
Improved reusability of the code
Multiple classes with the same members can be placed in the same class
Improved Code maintainability
If the code of the function needs to be modified, it can be changed in one place, and the inherited class will be modified automatically.
Having a relationship between classes and classes is a precondition for polymorphism
Characteristics of inheritance in 5.Java
- Java only single inheritance is supported and multiple inheritance is not supported.
That is, a class can have only one direct parent class, and cannot have more than one direct parent class.
Class Subdemo extends demo{}//ok
Class Subdemo extends Demo1,demo2...//error cannot inherit multiple classes
- Java supports multiple layers of inheritance ( inheritance system )
Class a{}
Class B extends a{}
Class C extends b{}
Considerations for Inheritance in 6.Java
Subclasses can only inherit all non-private members of the parent class (member methods and member variables)
Subclasses cannot inherit the constructor of the parent class, but can access the parent class construction method through the super (later) keyword.
Do not inherit for part of the function
When are we going to use inheritance?
Among the classes in the inheritance is the relationship of "is a".
7. Relationship of member variables in inheritance
Case Demo
member variables with the same name and different names in the child parent class
The order of finding variables in a subclass method
The composition of the class:
Member variables:
Construction Method:
Member Methods:
And now we are explaining inheritance, so we should consider the respective relationships of the constituent parts of the class.
Relationship of member variables in inheritance:
A: The member variable in the subclass is not the same as the member variable name in the parent class, this is too simple.
B: What happens when a member variable in a subclass is the same as a member variable name in a parent class?
To access the lookup order of a variable in a subclass method:
A: Find the local scope of the subclass method, and use it
B: In the subclass of the member scope to find, there is the use of
C: In the member scope of the parent class, you can use the
D: If you can't find it, you'll get an error.
8. Conclusion
Accessing a variable in a subclass method
First, in the sub-class local scope, that is, the method interior
And then find it in the subclass member scope, which is the member variable of the child class
Finally found in the scope of the parent class member variable (must not access the parent class local scope)
If you still do not have an error.
9.super keywords
Super the usage and This much like
This represents a reference to this class of objects
Super represents the identity of the parent storage space (which can be understood as a reference to the parent class object)
Usage (this and super can be used as follows)
- Accessing member variables
this. Member variable
Super. Member variable (accessing the member variable of the parent class, cannot access the private variable of the parent class)
When you access a static member, you can also use: Parent class name. Static members
- Access Construction Methods ( The construction method of the child parent class is a matter of speaking again )
This (...) Super (...) //
- Accessing member Methods ( The member method of the child parent class again )
This. Member method () Super. Member Method ()
10. The problem is:
I'm not just outputting the local range of num, I'm also outputting num of this class member range. What do we do?
I also want to output num of the parent class member range. What do we do?
If there is one thing similar to this, but you can directly access the data of the parent class.
Congratulations, this keyword is there: Super.
What is the difference between this and super?
This represents the corresponding reference for this class.
Super represents the identity of the parent storage space (which can be understood as a parent class reference that can manipulate members of the parent class)
A: Calling member variables
This member variable calls the member variable of this class
Super. member variables call member variables of the parent class
B: Call construction method
This (...) Calling this class's constructor method
Super (...) constructor method for calling parent class
C: Call member Method
This member method calls the member method of this class
Super. Member methods call the parent class's Members method
Big Data Java Foundation Day