Inheritance in Java can only be single inheritance, and multiple inheritance is not allowed. This is different from C++, but Java can realize the function of multiple inheritance through the implementation of interfaces; Java can implement multiple interfaces at one time;
1. First define a parent Class Person. The subclass is Man;
The member methods and properties are as shown below;
2. Definition of subclass
3. As shown above, we have defined the parent class and subclass; we will implement the main method below;
4. When instantiating a subclass, the constructor of the parent class will be called first, and then the constructor of the subclass will be called;
After a while, let’s take a look at the running screenshots;
5. Let’s take a look at the overall running screenshots;
6. Let’s analyze it. The use of inheritance can greatly improve the utilization of code and prevent the redundancy of code. Remain.
is that the subclass automatically inherits all attributes and methods of the parent class except private; the subclass does not need to be defined and can be used directly. Let’s analyze it carefully.
The above is the detailed content of Detailed explanation of inheritance in Java. For more information, please follow other related articles on the PHP Chinese website!