2.1.11 · D1OOP Fundamentals

Foundations — Method overriding — when and why

1,622 words7 min readBack to topic

Before you can enjoy the parent topic, you need every word and symbol it leans on. This page builds each one from nothing, in the order that lets the next one make sense.


0. What is a "class" and an "object"?

Everything below rests on these two words, so we earn them first.

Think of a cookie cutter versus the cookies. The cutter is the class; each cookie you stamp out is an object.

Figure — Method overriding — when and why

1. A "method" and its "signature"

The dot . is just plain English "'s": circle.area() = "the circle's area action, run it now". The parentheses () mean actually do it now (and hold any inputs).

Figure — Method overriding — when and why

2. Inheritance and the Parent → Child relationship

A Circle is-a Shape. So a Circle already owns everything a Shape promised, for free.


3. Two type words: declared vs runtime

This is the trickiest pair, and the parent note's whole "which version runs?" question turns on it.

In Shape s = new Circle();

  • declared type of s is Shape (the label on the box),
  • runtime type of the object is Circle (what's inside the box).
Figure — Method overriding — when and why

4. The lookup table (vtable) and the map notation

The parent note writes:

Let's decode every glyph.

Read the whole line as plain English: "class 's lookup table pairs each method name with the newest (most-derived) version of that method visible from ."

Figure — Method overriding — when and why

5. Symbols borrowed from the geometry example

The parent illustrates with shapes, so two tiny math symbols sneak in:



How the foundations feed the topic

class vs object

method and signature

inheritance parent child

declared type vs runtime type

vtable lookup name to code

method overriding dynamic dispatch

Open Closed Principle

Liskov Substitution

Read it bottom-up: overriding needs the vtable, which needs both a signature (the row name) and the runtime type (which table). Those in turn need the class/object split and inheritance. The two principles hang off the side as why we bother.

You are now equipped to read the parent's "HOW it works — derive dynamic dispatch" section without any symbol being new. Related siblings once you're comfortable: Polymorphism, Method overloading, Abstract classes and interfaces, super keyword.


Equipment checklist

Cover the right side; can you answer each before revealing?

A class is a ___ and an object is a ___
A class is a blueprint; an object is a real thing built from it (an instance).
A method's signature is made of ___
Its name plus the list/type of inputs it expects.
The . in obj.area() means ___ and () means ___
. means "this object's"; () means "run it now (with these inputs)".
"Child is-a parent" describes which relationship?
Inheritance — the child gets all the parent's methods for free.
In Shape s = new Circle();, the declared type is ___ and the runtime type is ___
Declared: Shape (the box label); runtime: Circle (what's inside).
Which type decides which overridden method actually runs?
The runtime (actual) type of the object.
The symbol is read as ___
"maps to" — it pairs a name on the left with a value/code on the right.
means ___
Look up method name in the lookup table of the object's runtime class .
A child's vtable is built by ___
Copying the parent's table, then overwriting the rows it overrides.
The Open/Closed Principle says code should be ___ to extension and ___ to modification
Open to extension, closed to modification.