Home > Java > javaTutorial > body text

Detailed explanation of inheritance in Java

黄舟
Release: 2017-08-04 09:20:01
Original
1387 people have browsed it

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;

Detailed explanation of inheritance in Java

2. Definition of subclass

Detailed explanation of inheritance in Java

3. As shown above, we have defined the parent class and subclass; we will implement the main method below;

Detailed explanation of inheritance in Java

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;

Detailed explanation of inheritance in Java

5. Let’s take a look at the overall running screenshots;

Detailed explanation of inheritance in Java

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template