Skip to main content

Practice ยท 2 of 3

Sealed hierarchy with exhaustive dispatch

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.

Difficulty: advanced

Back to lesson: Practice: Object model drills