简体   繁体   中英

Closeable extends AutoCloseable

I was recently going through the src.zip file of jdk 1.8 and found out something quite odd.

public interface Closeable extends AutoCloseable 

and since AutoCloseable is an interface as well, how is anything in java allowed to extend it?

When i tried it with any other interfaces, i was constantly getting the error message :

The type Runnable cannot be the superclass of userDefine Class ; a superclass must be a class.

So, why this discrepancy? or am i missing something ? If so, please explain it to me.

If you just think of it in terms of natural language, interfaces can extend other interfaces because they are extending the requirements they give to an implementing class. They are more specific contracts.

If an interface were to use the implements keyword to express this idea then it would be confusing. 'Implements' means 'to provide an implementation' and that's not what's happening here.

Hence:

  1. interfaces can extend interfaces
  2. classes can extend classes
  3. classes can implement interfaces

Interfaces extends interfaces. Not implements .

When inheriting, you use the keyword extends , both when a class inherits from a class or an interface inherits from another interface.

When it comes to interfaces, the keyword extends can be seen as implements for classes.

This will add the capabilites of the super interface to the extendee

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