When we are preparing to write a class, we find that a certain class has the member variables and methods we need. If we want to reuse the member variables and methods in this class, we do not need to declare member variables and definitions in the class we write. Method is equivalent to having this member variable and method, then we can define the class we write as a subclass of this class.
Inheritance is a mechanism for creating new classes from existing classes. Using inheritance, we can first define a general class with common attributes, and then define a subclass with special attributes based on the general class. The subclass inherits the attributes of the general class. and behaviors, and add its own new properties and behaviors as needed. The class obtained by inheritance is called a subclass, and the inherited class is called a parent class (super class).
Notice:
Java does not support multiple inheritance, that is, a subclass can only have one parent class. People are accustomed to calling the relationship between a subclass and a parent class an "is-a" relationship.
In the declaration of a class, a subclass of a class is defined by using the keyword extends. The general format is:
class subclass name extends parent class name {…}
For example:
classStudentextendsPeople{…}
Define the Student class as a subclass of the People class, and the People class is the parent class (super class) of the Student class.
Class tree structure:
If C is a subtype of B, and B is a subtype of A, it is customary to call C a descendant of A. Java classes form a tree structure according to inheritance relationships (think of classes as nodes on the tree). In this tree structure, the root node is the Object class (Object is a class in the java.lang package), that is, Object is The ancestor class of all classes. Any class is a descendant of the Object class. Each class (except the Object class) has one and only one parent class. A class can have multiple or zero subclasses.
Notice:
If the extends keyword is not used in the declaration of a class (except the Object class), the class is defaulted to a subclass of Object by the system. For example: the class declaration "class A" is equivalent to "class A extends Object".