简体   繁体   中英

Why can not inherit from Interface

In Java, why can not I inherit from Interfaces?

If the case is that the interfaces do no provide the implementation of the methods, then how are we able to inherit from Abstract Classes.

Simply put, you cannot inherit from interfaces, because interfaces provide only abstraction, not functionality .

Notes from the comments below (thanks to @Mark Peters and @qqilihq) :

  • Interfaces can inherit from interfaces
  • Both abstract classes and interfaces are forms of inheritance, but currently only abstract classes support code inheritance. Interfaces provide type inheritance. In Java 8, interfaces will provide code inheritance as well.

You can also check the Official Java Tutorials , it's a good starting point for Java knowledge.

This is because abstract classes and interfaces are inherently different types of objects. Abstract classes define functionality while inheritances act as a framework.

EDIT: As I posted above you can inherit as many interfaces as you want but only a single abstract class

Java supports only single inheritance via the EXTENDS keyword. The idea is that you can "inherit" from multiple interfaces in the same class.

Therefor the IMPLEMENTS keyword is used to "inherit" an interface.

A class that IMPLEMENTS an interface gains access to all declarations in the interface.

As a matter of fact you can inherit from interfaces - but only to extend their interface.

You can create abstract classes that do some or all of the work required by an interface.

You can then extend your abstract classes to make real classes. Even overriding functionality from the abstract class if you wish.

// close
interface Closeable {
  public void close();

}

// open and close
interface Openable extends Closeable {
  public void open();

}

// Simplistic implementation
abstract class AbstractCloseable implements Closeable {

  @Override
  public void close() {
    System.out.println(this.getClass().getSimpleName() + "-close");
  }

}

// Simplistic implementation
abstract class AbstractOpenable extends AbstractCloseable implements Openable {
  boolean open = false;

  @Override
  public void open() {
    System.out.println(this.getClass().getSimpleName() + "-open");
    open = true;
  }

  @Override
  public void close() {
    super.close();
    open = false;
  }

}

class PortHole extends AbstractOpenable implements Openable {

  public void clean() {
    if (!open) {
      System.out.println(this.getClass().getSimpleName() + "-clean");
    } else {
      System.out.println(this.getClass().getSimpleName() + "-fall out");

    }
  }

}

public void test() {
  System.out.println("Hello");
  PortHole p = new PortHole();
  p.open();
  p.clean();
  p.close();
  p.clean();
}

Prints:

PortHole-open
PortHole-fall out
PortHole-close
PortHole-clean

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