Code Poet
Code Poet

Reputation: 11437

extending an interface instead of implementing it

An interface definition (see below) has me confused:

public abstract interface Cursor {
    // hide details
}

public abstract interface Parcelable {
    // hide details
}

public interface Foo<T extends Bar> extends Cursor, Parcelable {
    // details omitted for brevity
};
  1. I thought Java doesn't have multiple inheritance, so a class cannot extends more than one class.
  2. On the other hand a class/interface can implement more than one interface. So why use extends instead of implements?

Upvotes: 2

Views: 914

Answers (3)

Bruno Reis
Bruno Reis

Reputation: 37822

The rules for class/interface inheritance and implementation in Java 7 and below are as follows:

  1. A class can only inherit from a single class (single inheritance)
  2. A class might implement one or more interfaces (multiple interface implementation)
  3. An interface might inherit from one or more interface (multiple interface inheritance)

When inheriting, you use the keyword extends, both when a class inherits from a class or an interface inherits from one or more interfaces. The term extends is to be understood as follows: this class/interface is an extension of the its parent class/interface(s) -- it is everything the parent or parents are, and possibly more.

When a class implements an interface (or more than one), you use the keyword implements. The term implements is to be understood as follows: instances of this class are guaranteed to provide implementations for the methods of the parent interface(s).

Also, note that an abstract class uses the keyword implements when referring to a parent interface even if the abstract class itself does not implement the interface methods. This does not violate the principles stated above: there can only be instances of concrete classes, which must implement every declared method; therefore, any instance of that abstract class must be, in reality, an instance of a subclass implementing the methods from the interface. For example, this is perfectly valid: abstract class AnAbstractClass implements Cursor {}, even if Cursor declares lots of methods.

In your examples, the interface Foo<T extends Bar> inherits from two other interfaces, namely Cursor and Parcelable, which is an example of the 3rd point above. You use the keyword extends and not implements because the interface Foo is not implementing anything: the bodies of the methods of the parent interfaces are still not defined! If you had a class implementing those two parent interfaces, you'd do something like:

class AClass implements Cursor, Parcelable {
  @Override public // ... method signature and implementation
                   // for every method in Cursor and Parcelable
}

Also, note that you don't need to use the keyword abstract when declaring an interface: both the type and all its declared methods are implicitly abstract. The methods are also implicitly public (and cannot be declared with any other access modifier), since the purpose of an interface is precisely to declare the "public interface of an object", that is, which methods are guaranteed to be publicly available by any object implementing that interface.


Finally, some of the concepts described above are subject to be slightly changed in Java 8: in that release, you will be able to provide "default implementations" of methods right in the interface, in case the classes implementing the interfaces do not implement them. Things will be more similar to "class multiple inheritance": a class will be able to inherit implemented methods from multiple sources, which is not possible in Java 7 or below. For example (the syntax is not yet fixed):

interface NaturalNumber {
  void increment();
  default void add(int n) {
    for (int i = 0; i < n; i++) {
      increment();
    }
  }
}

class NaturalNumberImplementation implements NaturalNumber {
  private int n = 0;
  @Override public void increment() { n++; }
}

This exists to allow you to neglect implementing some methods when you don't want to, while still being able to implement "better" versions of them when possible/needed. For instance:

class HighPerformanceNaturalNumberImplementation implements NaturalNumber {
  private int n = 0;
  @Override public void increment() { n++; }
  @Override public void add(int n) { this.n += n; }
}

Upvotes: 14

jahroy
jahroy

Reputation: 22692

Interfaces:

  • Interfaces extend interfaces.

  • An interface can extend multiple interfaces.

Classes:

  • Clases extend classes.

  • A class can only extend a single class.

  • Only classes can implement interfaces.

  • A class can implement multiple interfaces.

Upvotes: 0

Juned Ahsan
Juned Ahsan

Reputation: 68715

Answer 1.

A Java class can only extend one parent class. Multiple inheritance is not allowed. Interfaces are not classes, however, and an interface can extend more than one parent interface.

Answer 2.

You should always extend a class/interface when there is a parent child relationship. There should be no other reason for extending a class or an interface.

Upvotes: 2

Related Questions