Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 38 ยท Classes and Objects

Inheritance, super and Polymorphism in Smalltalk

Understand Smalltalk single inheritance, method lookup, overriding, super, abstract methods with subclassResponsibility, and polymorphism with a shapes example you can follow and run as a JavaScript stand-in.

Inheritance lets a class reuse the methods and instance variables of another. Polymorphism lets you send the same message to different kinds of objects and get behaviour appropriate to each. Together they are the heart of object-oriented design, and Smalltalk's version is small and strict: single inheritance, dynamic method lookup, and no interfaces. This lesson explains how lookup works and how to design with it.

The Smalltalk code is read-only on this site; stand-ins are in JavaScript.

Subclassing

SMALLTALK
Object << #Shape
    slots: { #name };
    package: 'Shapes'.

Shape << #Circle
    slots: { #radius };
    package: 'Shapes'.

Shape << #Rectangle
    slots: { #width. #height };
    package: 'Shapes'.

Circle and Rectangle are subclasses of Shape. They inherit name and every method defined in Shape, and in Object above it. Every class has exactly one superclass (except ProtoObject), so the classes form a tree.

Method lookup

When an object receives a message, Smalltalk looks for a method with that selector in the object's class. If it finds none, it looks in the superclass, and so on up to Object. If nothing is found, it sends doesNotUnderstand:.

flowchart BT
    O["Object<br/>printString, isNil, ..."] 
    S["Shape<br/>describe, area (abstract)"] --> O
    C["Circle<br/>area"] --> S
    R["Rectangle<br/>area"] --> S
    Q["Square<br/>(inherits Rectangle)"] --> R

A message sent to a Circle is searched in Circle, then Shape, then Object.

You can ask the system: Circle superclass answers Shape, Circle allSuperclasses the whole chain, Shape subclasses the children, Circle inheritsFrom: Shape answers true.

Overriding

A subclass overrides an inherited method by defining one with the same selector:

SMALLTALK
Shape >> describe
    ^ 'A shape named ', name asString

Circle >> describe
    ^ 'A circle of radius ', radius printString

A circle now answers its own describe. Callers need not know which one will run. That is polymorphism.

Abstract methods

Shape says every shape has an area, but it cannot compute one. Smalltalk has no abstract keyword; instead you write a method that fails if reached, using subclassResponsibility:

SMALLTALK
Shape >> area
    ^ self subclassResponsibility

If someone forgets to override area, they get a clear error "My subclass should have overridden #area". The concrete classes provide the real thing:

SMALLTALK
Circle >> area
    ^ Float pi * radius squared

Rectangle >> area
    ^ width * height

super: reuse and extend

super sends a message to the same object but starts lookup in the superclass of the class where the method is defined (not in the receiver's class). Use it to extend rather than replace:

SMALLTALK
Circle >> describe
    ^ super describe, ' (circle, area ', self area printString, ')'

The classic use is initialize: super initialize first, then your own setup. Use super only for the same selector you are overriding (or occasionally another one); otherwise just send to self.

A famous bug: super is not "the parent object". There is only one object. If Square inherits Rectangle and Rectangle>>area calls self width, a Square can override width, and the Rectangle method will see the override, because self is the Square.

Polymorphism in practice

SMALLTALK
| shapes |
shapes := { Circle radius: 2. Rectangle width: 3 height: 4 }.
shapes do: [ :each | Transcript show: each describe; cr ].
shapes inject: 0 into: [ :sum :each | sum + each area ].

The loop contains no if each isKindOf: Circle. Each shape knows how to answer. When you find yourself writing type checks like that, it is a sign to move the behaviour into the classes. Adding Triangle later means writing one new class; the loop does not change.

This is also how Smalltalk replaces switch: instead of a case on a type tag, send a message and let each class decide.

Duck typing

Smalltalk does not check types. An object can be used anywhere a message it understands is sent, regardless of class. Circle and an unrelated Photo class can both answer area, and a function that only sends area will accept both. There are no interfaces; protocols are agreements on selectors. Traits (reusable method groups composed into a class) cover sharing without a common superclass.

Design advice

  • Inherit for "is a", compose for "has a". A Circle is a Shape; a Car has an Engine.
  • Keep hierarchies shallow; deep trees are hard to understand.
  • Put shared behaviour as high as makes sense, and make abstract methods explicit.
  • Prefer overriding small hooks (name, area) to copying large methods.
  • Use isKindOf: only in tests and tools, not in application logic.
JS Stand-in: shapes and polymorphism

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Stand-in: method lookup walks up the chain

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What happens when an object receives a message that its class does not define?

    Lookup climbs the hierarchy; if no method is found, doesNotUnderstand: is sent.
  2. 2. How do you declare an abstract method in Smalltalk?

    subclassResponsibility raises a clear error if the method is reached because a subclass did not override it.
  3. 3. What does super do?

    super is the same receiver as self; only the lookup start changes.
  4. 4. What is the best replacement for a type-based switch on shape kinds?

    Send the message and let each class answer for itself.

Summary

  • Each class has one superclass; the classes form a tree rooted at Object.
  • Lookup starts in the receiver's class and climbs until it finds the method, else doesNotUnderstand:.
  • Override to specialise; use super to extend; use subclassResponsibility for abstract methods.
  • Polymorphism removes the need for type checks: send a message and let the receiver decide.
  • Prefer composition for "has a", keep hierarchies shallow, and rely on duck typing and traits for sharing.

Next: Class Side, Class Variables and printOn: