Skip to lesson

Two classes can be linked in two ways. IS-A says one class is a kind of another and is written with extends. HAS-A says one object holds another as a part and is written with a field. Picking the right one is the first design decision for every new class.

HAS-A: an object with a part

The HAS-A relationship means one object owns another object as a field, as in "a Car has an Engine". It is written with a field, not with extends.

A city bus needs a driver, but a bus is not a driver. Why inheritance? (opens in a new tab) ended with this kind of link: instead of extends, the Bus keeps a Driver object in a field.

Example 1 · A bus has a driver

BusDemo.javaJava
class Driver {    private String name;
    Driver(String name) {        this.name = name;    }
    String getName() {        return name;    }}
class Bus {    private String number;    private Driver driver;
    Bus(String number, Driver driver) {        this.number = number;        this.driver = driver;    }
    void start() {        System.out.println("Bus " + number + " leaves the depot");        System.out.println("Driver: " + driver.getName());    }}
public class BusDemo {    public static void main(String[] args) {        Bus bus = new Bus("KA-01-F-1234", new Driver("Ravi"));        bus.start();    }}
Output
Bus KA-01-F-1234 leaves the depot
Driver: Ravi

What this code does

  • private Driver driver; is the HAS-A link: a field whose type is another class. Bus has no extends at all.
  • start() asks its part for the name with driver.getName(), instead of copying the driver's fields into Bus.

The outer object is called the whole and the object in its field is the part. Objects inside objects (opens in a new tab) shows what such a field really stores and how to reach the part safely.

The sentence test, both ways

The IS-A relationship is the link between a child class and its parent: every child object is also a kind of parent object, as in "a Student is a Person". Use inheritance only when this sentence is true.

Before you link two classes, say both sentences aloud: "a ___ is a ___" and "a ___ has a ___". The true sentence picks the design: extends for IS-A, a field for HAS-A.

Java
class Vehicle { }class Driver { }class Camera { }
class Bus extends Vehicle {     // a bus IS A vehicle    private Driver driver;      // a bus HAS A driver}
class Phone {    private Camera camera;      // a phone HAS A camera, but is not one}

One class can pass both tests with different classes. A bus is a vehicle, and a bus has a driver.

Class diagram. SmartPhone points to Phone with an arrow labelled "is a" (extends). SmartPhone is joined to Camera by a line with a diamond at the SmartPhone end, labelled "has a" (a private Camera field). One parent, any number of parts.

Example 2 · Both links in one class: a smartphone

SmartPhoneDemo.javaJava
class Phone {    void call(String to) {        System.out.println("Calling " + to);    }}
class Camera {    private int megaPixels;
    Camera(int megaPixels) {        this.megaPixels = megaPixels;    }
    void click() {        System.out.println("Photo taken at " + megaPixels + " MP");    }}
class SmartPhone extends Phone {    private Camera camera = new Camera(50);
    void takeSelfie() {        camera.click();    }}
public class SmartPhoneDemo {    public static void main(String[] args) {        SmartPhone p = new SmartPhone();        p.call("Priya");        p.takeSelfie();    }}
Output
Calling Priya
Photo taken at 50 MP

What this code does

  • SmartPhone extends Phone is the IS-A link, so call() comes from Phone. The camera field is the HAS-A link.
  • takeSelfie() uses the part with camera.click(). Outside code never touches the camera directly; it only sees the phone's own methods.

Java lets a class extend only one parent, as in One parent only (opens in a new tab), but a class may have as many parts as it needs. That is why most real classes have more HAS-A links than IS-A links.

When extends leaks the wrong methods

A stack is a pile where you add and remove only at the top, like plates in a canteen. Someone writes class Stack extends NumberList just to reuse the list's code. It compiles, but the stack now has every list method too.

Example 3 · Wrong inheritance breaks the stack

BadStackDemo.javaJava
class NumberList {    private int[] items = new int[10];    private int size;
    void add(int x) {        items[size] = x;        size++;    }
    int removeAt(int index) {        int x = items[index];        for (int i = index; i < size - 1; i++) {            items[i] = items[i + 1];        }        size--;        return x;    }
    int size() {        return size;    }}
class Stack extends NumberList {    void push(int x) {        add(x);    }
    int pop() {        return removeAt(size() - 1);    }}
public class BadStackDemo {    public static void main(String[] args) {        Stack plates = new Stack();        plates.push(1);        plates.push(2);        plates.push(3);        plates.removeAt(0);        System.out.println("Top: " + plates.pop());        System.out.println("Next: " + plates.pop());        System.out.println("Left: " + plates.size());    }}
Output
Top: 3
Next: 2
Left: 0

What this code does

  • Stack extends NumberList was written only to reuse add and removeAt. "A stack is a number list" is not a true sentence.
  • Because of extends, main can call removeAt(0) and pull out the bottom plate. No error appears: plate 1 is simply gone, and only two pops were possible.

Example 4 · The fix: the stack has a list

GoodStackDemo.javaJava
class NumberList {    private int[] items = new int[10];    private int size;
    void add(int x) {        items[size] = x;        size++;    }
    int removeAt(int index) {        int x = items[index];        for (int i = index; i < size - 1; i++) {            items[i] = items[i + 1];        }        size--;        return x;    }
    int size() {        return size;    }}
class Stack {    private NumberList list = new NumberList();
    void push(int x) {        list.add(x);    }
    int pop() {        return list.removeAt(list.size() - 1);    }
    int size() {        return list.size();    }}
public class GoodStackDemo {    public static void main(String[] args) {        Stack plates = new Stack();        plates.push(1);        plates.push(2);        plates.push(3);        System.out.println("Top: " + plates.pop());        System.out.println("Next: " + plates.pop());        System.out.println("Left: " + plates.size());    }}
Output
Top: 3
Next: 2
Left: 1

What this code does

  • The stack keeps the list in a private field and still reuses its code inside push and pop.
  • Stack now shows only push, pop and size. Writing plates.removeAt(0) gives a compile error, so the top-only rule is safe.

So IS-A needs a second check: the child must be able to do everything the parent does without breaking its own rules. "A stack is a number list" fails it, because the inherited removeAt(0) breaks the top-only rule.

When you are unsure, choose HAS-A. A field is easy to change later; a wrong extends spreads to every caller. Composition vs inheritance (opens in a new tab) looks at this choice in depth.

Next step

Check your understanding

11 quick questions