0Pricing
Java Academy · Lesson

Object Composition

Build bigger objects by combining smaller ones. Composition models has-a relations like a Car that has an Engine.

Object Composition is a free Java Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Definition

Composition means building a bigger object out of smaller objects it owns. It is a has-a relation: a Car has an Engine. This keeps classes small and focused, and the big object delegates work to its parts.

Benefits

Composition helps reuse: you can swap a part without changing the whole. It also improves testability because each part can be tested separately. Start simple: make one class depend on another through fields.

Composition vs inheritance

Inheritance is an is-a relation (a Square is a Shape). Composition is a has-a relation (a Car has an Engine). Prefer composition first; inherit only when a true is-a fits naturally.

Ownership & lifecycle

When an object owns its parts, it is responsible for creating and using them. Parts usually do not live alone; they exist because the whole exists. This idea keeps responsibilities clear.

Design steps

Pick the whole (Car) and the parts (Engine). List what each part knows and does. Then put part objects as fields inside the whole, and write small methods that use those parts to do work.

Code: has-a example

Code: Car owns an Engine and calls its method. Creating the part first and passing it in keeps the code simple and flexible.

public class Main {
  // Very small classes to show composition (has-a)
  static class Engine {
    int horsepower;
    Engine(int hp) { horsepower = hp; }
    String info() { return horsepower + " HP engine"; }
  }

  static class Car {
    String model;
    Engine engine; // Car has an Engine (composition)
    Car(String m, Engine e) {
      model = m;
      engine = e; // store the part
    }
    void printSpecs() {
      // Use the part to build a message
      System.out.println(model + " with " + engine.info());
    }
  }

  public static void main(String[] args) {
    // Create part first
    Engine e = new Engine(90);
    // Assemble whole with the part
    Car c = new Car("CityCar", e);
    // Use the whole, which delegates to its part
    c.printSpecs();
  }
}

Composition check

Quick check: Which statement describes composition best?

Recap

Recap: Composition models has-a. Start with a small part, give it a clear job, and let the whole object use it through simple method calls.

Frequently asked questions

Is the “Object Composition” lesson free?

Yes — the full text of “Object Composition” is free to read here on the web, and the Java Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “Object Composition”?

Build bigger objects by combining smaller ones. Composition models has-a relations like a Car that has an Engine. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Object Composition” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. this & toString/equals/hashCode
  2. Object Composition
  3. Modeling a Domain (UML→Code)
← Back to Java Academy