Java OOP – Chapter 9: Composition vs Inheritance in Java
T
Tuan Beo

Java OOP – Chapter 9: Composition vs Inheritance in Java

In this lesson, you’ll learn the difference between inheritance (an "is-a" relationship) and composition (a "has-a" relationship), and when to use each. Understanding these concepts helps you design more flexible, maintainable, and decoupled systems.

Inheritance Recap (is-a)

Inheritance allows a subclass to acquire the behavior (methods) and properties (fields) of a superclass.

Example:

class Animal {
    void makeSound() {
        System.out.println("Animal makes sound");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("Dog barks");
    }
}

A Dog is an Animal.

What is Composition? (has-a)

Composition means including an instance of one class inside another class to achieve code reuse.

Example:

class Engine {
    void start() {
        System.out.println("Engine started");
    }
}

class Car {
    Engine engine = new Engine(); // Composition

    void drive() {
        engine.start();
        System.out.println("Car is driving");
    }
}

A Car has an Engine.

Composition vs Inheritance

Feature

Inheritance

Composition

Relationship

Is-a

Has-a

Coupling

Tighter (strong dependency)

Looser (can change components)

Flexibility

Less flexible

More flexible

Reusability

Inherits entire class hierarchy

Reuses only needed components

Change risk

Changes in superclass affect child

Internal component changes less impactful

When to Use Each?

Use inheritance when:

  • You want to specialize a base type.

  • There's a true “is-a” relationship.

Use composition when:

  • You want to reuse functionality from multiple sources.

  • You want to encapsulate and replace components easily.

  • You favor flexibility over tight coupling.

Summary

  • Inheritance is powerful but can lead to fragile code if misused.

  • Composition offers better modularity and flexibility.

  • Prefer composition over inheritance unless there’s a clear “is-a” relationship.

Assignment: Lesson 9 – Composition vs Inheritance

Objective:

Understand when to use inheritance vs composition by modeling real-world objects.

Part 1: Conceptual Questions

Answer these briefly:

  1. What is the main difference between inheritance and composition?

  2. Why is composition considered more flexible?

  3. When should you prefer composition over inheritance?

Part 2: Coding Task

Step 1 – Inheritance:

Create a class Employee with:

  • Field: name (String)

  • Method: work()

Create a class Manager that extends Employee and:

  • Overrides work() to print: "Manager is planning projects"

Step 2 – Composition:

Create a class Address with fields:

  • street, city

Create a class Company that:

  • Has an Address field (composition)

  • Method printAddress() that prints the full address

In main():

  • Create a Manager object and call work()

  • Create a Company object with an Address, and call printAddress()

Github: https://github.com/tuanbeovnn/java-bootcamp/tree/java-bootcamp-oop-chapter9

Comments