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

Why do all Collection classes in Java inherit abstract classes and implement interfaces of abstract classes?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

Why do Collection classes in Java inherit abstract classes and implement abstract class interfaces? For this problem, this article describes the corresponding analysis and solutions in detail, hoping to help more partners who want to solve this problem find simpler and easier ways.

Recently, I saw a very popular project on Github, and star exceeded vue. I just wanted to check out github's star leaderboard. After reading it, I suddenly thought of whether I could check out the leaderboard on stackoverflow. It's good to do some translation too!

When I opened stackoverflow, I suddenly saw a very strange problem. Why do collections in Java inherit abstract classes and implement abstract class interfaces?

Why do many Collection classes in Java extend the abstract class and implement the interface as well?

That's a good question, one I've never paid attention to or thought about.

Translate roughly for everyone, why do you want to do this?

In Java, there are many collections that have such operations. For example: HashSet inherits AbstractSet and implements Set, but AbstractSet already implements Set. HashMap extends AbstractMap, AbstractMap implements Map, etc.

Why would you do that? According to the above explanation, one of the most important reasons is: regulation. It doesn't matter if we don't implement the abstract class, but simply implementing it helps us understand the code without going through the full hierarchy of a given class.

Second, their differences can be observed in reflection. For example, the following code:

After running, output the following result:

From the above results, you will find that List actually extends the Collection interface, but the Collection interface is not printed. That is, the output above does not include interfaces implemented by superclasses, nor does it include interfaces that are superinterfaces of included interfaces. For example, Iterable and Collection are omitted from above, even though ArrayList implicitly implements them. To find them, you must recursively iterate over the class hierarchy.

Fortunately, this difference does not affect instanceof. For example, new ArrayList() instanceof Iterable and Iterable.class.isAssignableFrom(ArrayList.class) results in true.

Readability is also an important part of the actual design and implementation of these collections in JDK.

About why the Collection class in Java inherits the abstract class and also implements the interface of the abstract class, the answer to the problem is shared here, I hope the above content can be of some help to everyone, if you still have a lot of doubts, you can pay attention to the industry information channel to learn more about knowledge.

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

Servers

Wechat

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

12
Report