Home > Java > javaTutorial > body text

Analysis of the advantages and disadvantages of Java inheritance

PHPz
Release: 2023-05-08 16:25:19
forward
1819 people have browsed it

1. Advantages

Improves the reusability of code.

Improves the maintainability of the code.

Let classes be related to classes, which is the prerequisite for polymorphism.

2. Disadvantages

Improve the coupling of classes. Changes to such a class will affect other classes related to this class.

Principle: low coupling, high internal aggregation.

Coupling: the relationship between classes.

Cohesion: The ability to accomplish something yourself.

Breaks encapsulation.

3. Inherited members

The inheritance mechanism introduces protected members and provides a new member access control level, which can be understood as public and private.

In inheritance, the subclass inherits all members except the super structure function, and these members become the inherited members of the subclass. Heirs include not only common, protected and private members of the super definition, but also super heirs.

In a subclass, the subclass can access all members defined by itself, and can also access the father's public and protected inherited members, but it cannot access the super private inherited members.

The access control of an inheritor in the subclass is the same as its access control in the superclass. And it is initially a public member in the super class, and is regarded as a public member after being inherited by the subclass; it is initially a protected member in the super class, and it is still a protected member after being inherited by the subclass; it is initially a private member in the super class , is regarded as a private member after being inherited by a subclass, but cannot be accessed by the subclass. An object whose data type is a subclass cannot access protected members of the subclass and its parent class.

What are the basic data types of java

The basic data types of Java are divided into:

1. Integer type, used to represent the data type of integer.

2. Floating point type, a data type used to represent decimals.

3. Character type. The keyword of character type is "char".

4. Boolean type is the basic data type that represents logical values.

The above is the detailed content of Analysis of the advantages and disadvantages of Java inheritance. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
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