2.1.15 · D1OOP Fundamentals

Foundations — Composition — has-a relationship vs is-a

1,474 words7 min readBack to topic

Before you can decide "is-a vs has-a", you must already know what a class, an object, a field, a method, inheritance, and delegation actually are — as pictures, not just words. The parent note assumed all of these. We build every one here from nothing.


0. The absolute starting point: what is an "object"?

Figure — Composition — has-a relationship vs is-a

Look at the figure: the outer box is one object. The top half (pale yellow) is its data — the facts. The bottom half (chalk blue) is its behaviour — the actions. Everything else in this topic is about how these boxes are made and connected.


In the parent's code, class Car: is the cutter. Car(Engine(120)) presses the cutter once and hands you one car cookie.


2. Field — a labelled slot inside the box

In self.engine = engine, the field is named engine and it stores... another object. Read that slowly:


3. Method — a button on the box

The def start(self): line defines a button. The self again means "the box this button is on".

Figure — Composition — has-a relationship vs is-a

The figure shows one Car box with a field slot holding an Engine box inside it, and buttons (start) on the outside. Notice the arrow: pressing the car's button reaches in to the engine box and presses its button. That reaching-in is the next concept.


4. Delegation — one box presses another box's button

The . (dot) is the tool that does this. car.start means "the start button belonging to car". self.engine.ignite means "reach into self, grab the engine field, press its ignite". The dot is just "belonging to / go inside".


The parent (or base) class is the old cutter; the child (or subclass) is a modified copy of that cutter that keeps every old button and may add more.

Figure — Composition — has-a relationship vs is-a

The figure contrasts the two wiring styles side by side:

  • Left (inheritance): the child box becomes an enlarged copy of the parent — one merged box, buttons inherited, connection frozen at the moment you write the code.
  • Right (composition): two separate boxes; the outer one merely holds the inner one in a slot and can pop it out and click a new one in.

6. Substitutability — the rule that makes "is-a" honest


Prerequisite map

Object = data plus behaviour

Class = blueprint

Field = named slot

Method = button

Field can hold another object

Delegation = press inner button

Inheritance = fancier cutter

Substitutability rule

has-a vs is-a decision

Read top to bottom: objects give us classes; classes have fields and methods; a field holding an object plus a method delegating to it gives composition; inheritance plus the substitutability rule gives the honest is-a; both feed the final decision this topic is about.


Equipment checklist

An object bundles which two things?
Data (facts it remembers) and behaviour (things it can do).
What does a class give you that an object does not?
The blueprint / cookie-cutter — one class stamps out many objects.
What does self mean inside a method?
"This particular object" — the specific instance the method is running on.
Can a field hold another object?
Yes — a slot can store a whole other box; this is the seed of composition.
What does the dot . do in car.engine.ignite()?
"Belonging to / go inside" — reach into car, grab its engine field, then press that engine's ignite button.
What is delegation?
A method does its job by calling a method on another object it holds, instead of doing the work itself.
What does inheritance automatically give the child class?
All the parent's fields and methods, with the relationship frozen at compile time.
What must be true for an honest "is-a"?
Substitutability — the child must work anywhere the parent is expected (Liskov).
Composition wiring vs inheritance wiring in one word each?
Composition = snap-together (loose, swappable); inheritance = welded (tight, permanent).

Connections

  • Parent: 2.1.15 Composition — has-a relationship vs is-a (Hinglish)
  • Next depth: Inheritance — is-a relationship, Liskov Substitution Principle
  • Related: Encapsulation and Information Hiding, Dependency Injection, Aggregation vs Composition (UML)