Sub topic 11
Interfaces That Extend Other Interfaces
An interface can build on another interface. You do this with the extends keyword, the same word you use when one class inherits from another class. The new interface gets every method of the old one and can add its own.
This lets you build small interfaces and combine them into bigger ones. It also lets you add new abilities without changing an interface other code already uses.
The Basic Idea
Say you have an interface called Animal. Every animal has a name. Some animals are pets, and a pet also has an owner. A pet is still an animal, so Pet can extend Animal.
A sub-interface is an interface that extends another interface. A super-interface is the one being extended. Here, Pet is the sub-interface and Animal is the super-interface.
Extending More Than One Interface
A class can extend only one class. An interface has no such limit: it can extend several interfaces at once. You list them after extends with commas between them.
The program below shows both cases.
interface Animal {
String name();
}
interface Pet extends Animal {
String owner();
}
interface Trackable {
String tagId();
}
interface ServiceDog extends Pet, Trackable {
String job();
}
class GuideDog implements ServiceDog {
@Override
public String name() {
return "Rex";
}
@Override
public String owner() {
return "Sam";
}
@Override
public String tagId() {
return "TAG-77";
}
@Override
public String job() {
return "guiding";
}
}
public class Main {
public static void main(String[] args) {
GuideDog dog = new GuideDog();
Animal a = dog;
Pet p = dog;
Trackable t = dog;
System.out.println(a.name()); // prints: Rex
System.out.println(p.owner()); // prints: Sam
System.out.println(t.tagId()); // prints: TAG-77
System.out.println(dog.job()); // prints: guiding
}
}GuideDog implements only ServiceDog. Because ServiceDog extends Pet and Trackable, and Pet extends Animal, the GuideDog object counts as all four types. You can store it in a variable of any of them.
The Class Must Implement the Whole Chain
When a class implements a sub-interface, it must provide every abstract method from that interface and from every interface above it. GuideDog has to write name(), owner(), tagId(), and job(). If even one is missing, the code will not compile, unless the class is declared abstract.
Quick Summary
| Declaration | Allowed? |
|---|---|
| `interface Pet extends Animal` | Yes |
| `interface ServiceDog extends Pet, Trackable` | Yes, several are allowed |
| `interface Pet implements Animal` | No |
| `interface Pet extends SomeClass` | No, interfaces extend only interfaces |
Exercise
You write class Cat implements Pet and give it only an owner() method. What happens, and how do you fix it?
Show answer
The code does not compile. Pet extends Animal, so Cat must also implement name(). You can fix it by adding a public name() method, or by declaring Cat as abstract.
Exercise
Write an interface Swimmer with a method void swim(), and an interface Diver that extends Swimmer and adds void dive(). Which methods must a class that implements Diver provide?
Show answer
interface Swimmer { void swim(); } and interface Diver extends Swimmer { void dive(); }. A class that implements Diver must provide public versions of both swim() and dive().