GCGrowCarrer

Polymorphism

Locked

Lesson 14 of 60 · OOP · Medium

Finish these first:Inheritance →Or pass the tests below to unlock it now.

1

The big idea

A reference of a supertype can hold subtype objects; method calls dispatch dynamically to the runtime type. Use instanceof pattern matching sparingly; prefer polymorphic methods.

Think of it like this: Think of a Class as a cookie cutter (the blueprint) and each Object as an individual cookie stamped from it — each cookie can have its own frosting (state) while sharing the same shape (methods).
Key words (tap to learn):
2

Read the lesson

A reference of a supertype can hold subtype objects; method calls dispatch dynamically to the runtime type.

List<Shape> shapes = List.of(new Circle(1), new Rect(2, 3));
for (Shape s : shapes) total += s.area();   // dynamic dispatch

Use instanceof pattern matching sparingly; prefer polymorphic methods.

3

Quick check

Before writing code: What should `Solution.totalArea(new Solution.Shape[]{() -> 2, () -> 3})` return when your implementation of Polymorphism is working?

4

Your challenge

2 sample tests, 1 hidden

Write static int totalArea(Shape[] shapes) using polymorphic calls.

Stuck? Get a hint (free)
More learning tools(compare languages, visual guide, related lessons)

Compare with another language

See how Java compares side-by-side with Python.

Visual walkthrough

A diagram, a step-by-step animation, and short captions for Polymorphism.

Mastery0%

Learn this first

Opens up next

This is the last lesson in this branch.