Composition

Composition builds one object from other objects. A Dashboard has a Clock, rather than pretending a dashboard is a clock.

This relationship keeps each class focused. Clock can own time-related behavior without inflating Dashboard.

Build an object from parts

class Clock {
    String time() {
        return "09:30";
    }
}

class Dashboard {
    private final Clock clock;

    Dashboard(Clock clock) {
        this.clock = clock;
    }

    void show() {
        System.out.println("Time: " + clock.time());
    }
}

public class Main {
    public static void main(String[] args) {
        Clock clock = new Clock();
        Dashboard dashboard = new Dashboard(clock);
        dashboard.show();
    }
}

Output:

Time: 09:30

The dashboard stores the Clock reference supplied to its constructor. Its show method asks the clock for the current time.

Prefer composition for most code reuse. Inheritance is appropriate only when the subtype genuinely satisfies the parent type's contract.

Store a Processor inside Computer and use its name in the computer description.