1. What is inheritance?
A mechanism that enables one class to have all the public properties and behaviors of another class.
2. The purpose of inheritance
If a class has all the behaviors and attributes of another class, and the number of these attributes and behaviors is large , and is shared by other classes. You can define this class as a subclass to inherit another class to achieve code reuse.
3. The impact of inheritance
The subclass has the non-private methods and properties of the parent class.
4. Restrictions on inheritance
# Constructor method: There is a default no-argument constructor in any class method, once a parameterized constructor is explicitly created, the default parameterless constructor is cleared. The parameterless constructor of the subclass calls the parameterless constructor of the parent class by default. If there is no parameterless constructor in the parent class, compilation will not pass. Supplement: There are constructors in abstract classes, but there are no constructors in interfaces. Initialize member variables first, then call the constructor.
# Single inheritance: In Java, a class can only inherit one parent class through the extends keyword.
# Method Override: Reference.
Coupling degree: The subclass inherits the parent class. The subclass has a greater dependence on the parent class. Changes in the parent class have a greater impact on the subclass. big.
5. Use conditions
#Because a class has only one inheritance opportunity, and inheritance will increase the degree of coupling, out of In order to maintain inheritance opportunities and reduce coupling, only consider using inheritance when one class has many methods in common with another class. Otherwise, you can consider implementing an interface or adopting a design pattern, such as the decorator pattern.
6. Interface inheritance
An interface can inherit multiple interfaces, so that it has abstract methods of other interfaces, and does not need to implement these itself. Inherited abstract methods.
The above is the detailed content of What is inheritance?. For more information, please follow other related articles on the PHP Chinese website!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver CS6
Visual web development tools