Sub topic 14
Choose Inheritance or Composition
Inheritance and composition are two ways to reuse code in Java. Inheritance means a class gets the fields and methods of another class. You write this with extends. Composition means a class holds an object of another class in one of its fields. The outer class then asks that inner object to do the work.
Both are useful. Choosing the wrong one makes your code harder to change later.
The Is-A and Has-A Test
A simple question helps you choose:
- Is-a: Is the new class a specific kind of the other class? If yes, inheritance may fit. A
Dogis anAnimal. - Has-a: Does the new class contain or use the other class? If yes, composition fits. A
Carhas anEngine.
A Worked Example: A Stack
A stack is a list where you add and remove items only at the top. Think of a stack of plates. The last plate you put on is the first one you take off.
Java's ArrayList can store items, but it also lets you add or read items anywhere. If Stack extended ArrayList, every ArrayList method would become part of Stack. Callers could insert items in the middle, which breaks the stack rule.
Composition avoids this. The Stack holds an ArrayList in a private field, which means other classes cannot touch it directly. The Stack offers only the methods it chooses.
import java.util.ArrayList;
class Stack {
private final ArrayList<String> items = new ArrayList<>();
void push(String item) {
items.add(item);
}
String pop() {
return items.remove(items.size() - 1);
}
boolean isEmpty() {
return items.isEmpty();
}
}
public class Main {
public static void main(String[] args) {
Stack stack = new Stack();
stack.push("first");
stack.push("second");
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.isEmpty());
}
}The program prints second, then first, then true.
Why Composition Often Wins
Inheritance gives your class every public method of the parent. Composition gives your class only the methods you write. Composition also lets you change the inner object later without changing what callers see.
When Inheritance Fits
Use inheritance when the is-a test passes and your class is a more specific version of the parent. A Dog can reuse the Animal class and add its own behavior.
> Hmm, do not write the line above; instead use the callout below.
| Question | Points toward |
|---|---|
| Is the new class a specific kind of the old one? | Inheritance |
| Does the new class need only part of the old class's work? | Composition |
| Should callers see every method of the other class? | Inheritance |
| Should you hide the internal parts? | Composition |
Exercise
A class named Library needs to keep a list of Book objects and offer addBook and listBooks methods. Should Library extend ArrayList<Book>, or should it hold an ArrayList<Book> in a private field?
Show answer
Hold the list in a private field (composition). A Library is not a kind of list, so the is-a test fails. A private field also lets Library control how books are added and listed, and hides the rest of the list methods from callers.