Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Java inner class, why do you need inner class?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report