Sub topic 13
Distinguish Extending a Class from Implementing Interfaces
A Java class can get behavior from another source in two ways. It can extend a class, or it can implement an interface. The two words look alike, but they do different jobs. This chapter shows how they differ and when to use each.
Two Ways to Share
When a class extends another class, it becomes a subclass of that class. The class it extends is its superclass, or parent. The subclass gets the parent's fields and code, so it does not have to write them again.
An interface is a list of method names that a class promises to write. An interface does not hold real code for those methods. A class that uses implements must write each method itself.
| Feature | extends | implements |
|---|---|---|
| Where it comes from | A class | An interface |
| How many you can use | One | As many as you need |
| Brings in code? | Yes, inherited code and fields | No, only a promise of method names |
| Constructor rule | Calls `super(...)` to reach the parent's constructor | Interfaces have no constructors |
A Worked Example
interface Swimmer {
void swim();
}
class Animal {
String name;
Animal(String name) {
this.name = name;
}
void eat() {
System.out.println(name + " is eating");
}
}
class Dog extends Animal implements Swimmer {
Dog(String name) {
super(name);
}
@Override
public void swim() {
System.out.println(name + " paddles across the pond");
}
}
public class Main {
public static void main(String[] args) {
Dog rex = new Dog("Rex");
rex.eat(); // inherited from Animal
rex.swim(); // promised by Swimmer, written in Dog
Swimmer s = rex; // a Dog is also a Swimmer
s.swim();
}
}The program prints:
Rex is eating
Rex paddles across the pond
Rex paddles across the pondThe eat() method comes from Animal, because Dog extends it. The swim() method comes from Swimmer, but Dog has to write its own version. The last two lines show that a Dog can be used anywhere a Swimmer is expected.
Why the Difference Matters
- A class can extend only one parent. Java does not allow two superclasses. This rule keeps it clear which fields and code a class inherits.
- A class can implement many interfaces. Use this when a class needs several abilities, such as swimming, flying, and running.
- Interfaces hold no per-object data. They can store constants, but each object's own data lives in its class.
> Note: Since Java 8, an interface can include default methods, which contain code. Even so, interfaces still have no constructors, so a class never calls super(...) to reach an interface.
Choosing Between Them
Ask yourself two questions:
- Is this a kind of thing that shares data and code with a parent? Use
extends. - Can this thing do a job, no matter what family it belongs to? Use
implements.
> Warning: If a class implements an interface but leaves out one of its methods, Java stops with an error before the program runs. Java checks that every promised method is written.
Practice
Exercise
A class Car extends Vehicle and implements Insurable and Sellable. How many superclasses can Car have, and how many interfaces?
Show answer
Car has one superclass (Vehicle) and two interfaces (Insurable and Sellable).
Exercise
Why does Java allow a class to implement many interfaces but extend only one class?
Show answer
Extending brings in fields and code, and two parents could give conflicting code. Interfaces mostly promise method names, so using several of them is simpler.