简体   繁体   中英

Why do Java interfaces have method implementations?

I know it might be a silly question but I can't find an answer.

As I understand interfaces in Java have method declarations only but can't have method implementations. But if we take a look at the Java API, there are a plenty of interfaces which have methods implementations. I understand that this could be due to inner classes.

But when I tried to look through source code, I saw that a lot of interfaces have methods which work without being overriden.

For instance NodeList interface, it "provides the abstraction of an ordered collection of nodes, without defining or constraining how this collection is implemented." You can look at source code and everything you can find is just comments. Do methods of this interface have native implementation?

And why do we need interfaces which have method implementations, it is too inconsistent. I can't understand it.

NodeList actually has a lot of implementations: https://xerces.apache.org/xerces-j/apiDocs/org/w3c/dom/class-use/NodeList.html

I think in your code where you are retrieving a NodeList, if you call .getClass().toString() on this, you'll find that you are dealing with one of the implementations listed on the link I provided

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM