Concept of inheritance: Create a new class based on the type of an existing class, Without changing the form of the existing class, this approach is called inheritance.
The role of inheritance: After you first create a class P, you now need to create a new class S, but the function is similar to P. At this time, if we want to be lazy and don't want to rewrite S, we can do it through inheritance.
Inheritance relationship: The inheritance relationship is divided into parent class (base class) and child class (derived class). The parent class is the inherited object (such as P), and the subclass is the implemented inherited object (such as S).
Inheritance method: Class inheritance is Single inheritance, that is, after S inherits P, it cannot inherit P2.
Characteristics of inheritance: The subclass will automatically obtain all the fields (variables) and methods in the parent class through inheritance.
Implementation of inheritance: Inheritance is implemented in Java through the keyword extends.
class Parent { } class Son extends Parent { }
The inheritance relationship in Java belongs to the IS-A relationship.
How to understand the IS-A relationship, take the above example: S inherits P, we can say that S is P.
In an inheritance relationship, the inheritor can completely replace the inheritee, but not vice versa. In a word, we "can treat people as animals, but we cannot treat animals as people".
A class is initialized by calling the constructor. For inheritance, the constructors of subclasses and parent classes have the following characteristics:
When the subclass inherits the parent class, if the constructor of the parent class is implicitly constructed Device, you don’t need to call it manually.
When a subclass inherits a parent class, if the constructor of the parent class is an explicit constructor, it must be called manually.
When a subclass inherits a parent class, the order of constructor calls is always from the parent class downwards.
Let’s explore it through an example:
class Parent { // 无参构造器,即隐式构造器。 public Parent() { System.out.println("initializing Parent"); } } class Son extends Parent { // 带参构造器,属显式构造器 public Son(String name) { // 关键 -> 由于父类是隐式构造器,这里可以不调用。 System.out.println("initializing "+name); } } class Grandson extends Son { public Grandson() { // 关键 -> 父类是显式构造函数,必须手动调用 super("son"); System.out.println("initializing Grandson"); } }public class Test { public static void main(String[] args) { Grandson grandson = new Grandson(); // 关键 -> 构造器调用顺序总是从父类依次往下进行的,打印内容如下: // initializing Parent // initializing Son // initializing Grandson } }
Inheritance Concept : Create a new class based on the type of an existing class without changing the form of the existing class. This method is called inheritance.
The role of inheritance: After you first create a class P, you now need to create a new class S, but the function is similar to P. At this time, if we want to be lazy and don't want to rewrite S, we can do it through inheritance.
Inheritance relationship: The inheritance relationship is divided into parent class (base class) and child class (derived class). The parent class is the inherited object (such as P), and the subclass is the implemented inherited object (such as S).
Inheritance method: Class inheritance is Single inheritance, that is, after S inherits P, it cannot inherit P2.
Characteristics of inheritance: The subclass will automatically obtain all the fields (variables) and methods in the parent class through inheritance.
Implementation of inheritance: Inheritance is implemented in Java through the keyword extends.
class Parent { } class Son extends Parent { }
The inheritance relationship in Java belongs to the IS-A relationship.
How to understand the IS-A relationship, take the above example: S inherits P, we can say that S is P.
In an inheritance relationship, the inheritor can completely replace the inheritee, but not vice versa. In a word, we "can treat people as animals, but we cannot treat animals as people".
A class is initialized by calling the constructor. For inheritance, the constructors of subclasses and parent classes have the following characteristics:
When the subclass inherits the parent class, if the constructor of the parent class is implicitly constructed Device, you don’t need to call it manually.
When a subclass inherits a parent class, if the constructor of the parent class is an explicit constructor, it must be called manually.
When a subclass inherits a parent class, the order of constructor calls is always from the parent class downwards.
Let’s explore it through an example:
class Parent { // 无参构造器,即隐式构造器。 public Parent() { System.out.println("initializing Parent"); } } class Son extends Parent { // 带参构造器,属显式构造器 public Son(String name) { // 关键 -> 由于父类是隐式构造器,这里可以不调用。 System.out.println("initializing "+name); } } class Grandson extends Son { public Grandson() { // 关键 -> 父类是显式构造函数,必须手动调用 super("son"); System.out.println("initializing Grandson"); } }public class Test { public static void main(String[] args) { Grandson grandson = new Grandson(); // 关键 -> 构造器调用顺序总是从父类依次往下进行的,打印内容如下: // initializing Parent // initializing Son // initializing Grandson } }
The above is the content of 02.Java Basics - Inheritance. For more related content, please pay attention to the PHP Chinese website (www.php .cn)!