Skip to main content

Practice · 2 of 3

Sealed hierarchy with exhaustive dispatch

CHALLENGE
Difficulty: advanced+25 XP

Given sealed interface Shape permits Circle, Rect, implement: 1. record Circle(double r) implements Shape and record Rect(double w, double h) implements Shape 2. static double area(Shape s) using a pattern switch with NO default clause 3. record Box(Shape shape) — a record *containing* a sealed type The switch must stay exhaustive: the compiler must guarantee that adding a new Shape variant breaks compilation until area handles it.

Back to lesson: Practice: Object model drills