WorkspaceLow-Level Design
← Course overview

Course navigation

On this page

Low-Level Design / OOP Fundamentals

foundation · 12 MIN READ

Polymorphism

Polymorphism lets the same operation select behavior from the object receiving it. A caller can process different implementations without an expanding switch on concrete type names.

Reading completion is independent of quizzes and challenges.

What you’ll understand

  • Polymorphism lets the same operation select behavior from the object receiving it. A caller can process different implementations without an expanding switch on concrete type names.
  • Each shape owns area(), and the report sums results through that operation.
  • Evaluate the tradeoff: Dynamic dispatch keeps callers stable, but each implementation still needs a shared meaning for units and valid dimensions. Polymorphism is not permission to return unrelated shapes of data.

Useful first: Classes and Objects

The idea and when to recognize it

Polymorphism lets the same operation select behavior from the object receiving it. A caller can process different implementations without an expanding switch on concrete type names.

  • Requirement: A drawing report adds areas of rectangles and squares using one area operation.
  • Change to anticipate: Handle a mixed collection of shapes without assuming that every shape is a square.
  • Design goal: Each shape owns area(), and the report sums results through that operation.

Start with the concrete problem

A drawing report adds areas of rectangles and squares using one area operation.

Handle a mixed collection of shapes without assuming that every shape is a square.

Direct implementationRevised design
Using width squared for every shape silently treats rectangles as squares.Each shape owns area(), and the report sums results through that operation.
The baseline case can hide the problem. Compare the extension and boundary fixtures before accepting this design.Dynamic dispatch keeps callers stable, but each implementation still needs a shared meaning for units and valid dimensions. Polymorphism is not permission to return unrelated shapes of data.

Responsibilities and contracts

Each shape owns area(), and the report sums results through that operation.

  • AreaReport: Aggregates the shared operation. Public operations: total(shapes)
  • Rectangle: Computes rectangular area. Public operations: area()
  • Square: Computes square area. Public operations: area()

Read the design

Select a participant to inspect its responsibility and public operations. Follow the labeled relationships; an arrow indicates direction, while the legend distinguishes ownership, substitution, and temporary use.

Preparing the design walkthrough…

Follow the interaction

Choose a supplied workload and compare the direct and revised implementations. Step through the same operation in both languages. Object values are recorded after each highlighted statement; previous steps remain unchanged.

Preparing the design walkthrough…

Implementation and expected output

Read the complete example alongside its input and expected output. The implementation selector changes the displayed program; the language selector preserves your current scenario and playback position.

Preparing the design walkthrough…

Try the idea in another setting

A payroll report can ask each payment rule for its amount without knowing hourly or salaried implementation details.

  • Name the new client contract before adapting the example.
  • Identify which state belongs to each object and who controls its lifetime.
  • Write one normal case and one boundary case before extending the design.

Tradeoffs and interview takeaways

Dynamic dispatch keeps callers stable, but each implementation still needs a shared meaning for units and valid dimensions. Polymorphism is not permission to return unrelated shapes of data.

Common mistake: Using width squared for every shape silently treats rectangles as squares.

  • Explain the choice: Each shape owns area(), and the report sums results through that operation.
  • Challenge your choice: Dynamic dispatch keeps callers stable, but each implementation still needs a shared meaning for units and valid dimensions. Polymorphism is not permission to return unrelated shapes of data.
  • Describe a requirement that would make the simpler implementation sufficient.

Design challenge and knowledge check

Preparing the design walkthrough…

Make it your own

Saved on this device · 0/20,000

Reading completion is independent of quizzes and challenges.