Get the App
SLTechnology News&Howtos  ›  Database  › 

Java inner class, why do you need inner class?

Shulou Source: shulou.com Published: 2022-06-01 07:13:36 09月26日 Update

Why do you need inner classes? Solved the problem of multiple inheritance, inheriting concrete or abstract classes.

In general, an inner class inherits from a class or implements an interface, and the code of the inner class operates to create the objects of its outer class. So it can be thought that the inner class provides some kind of window into its outer class.

The most attractive reason for inner classes is that each inner class can independently

Inherits from an implementation of an interface, so whether or not the outer class inherits an implementation of the interface has no effect on the inner class.

Without the ability of internal classes to inherit multiple concrete or abstract classes, some design and programming problems are difficult to solve. From this point of view, inner classes make the solution of multiple inheritance complete. The interface solves part of the problem, while the inner class effectively implements "multiple inheritance". That is, inner classes allow you to inherit multiple non-interface types.

Consider a situation where two interfaces must be implemented in one class in some way. Because of the flexibility of the interface, you have two choices: use a single class or use an inner class. But if you have an abstract class or a concrete class instead of an interface, you can only use internal classes to achieve multiple inheritance.

Using inner classes, you can get some other features as well:

-an inner class can have multiple instances, each with its own status information and independent of the information of its peripheral class objects.

-in a single peripheral class, multiple inner classes can implement the same interface or inherit the same class in different ways.

-the time when the inner class object is created does not depend on the creation of the outer class object.

-the inner class does not have a confusing is-a relationship, it is a separate entity.

Tags: Interfaces multiple objects problems independence information instances ways different valid there are in general two that is code single cause entity is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Shulou Technology OPPO Reno MySQL Apple