GCGrowCarrer

Enums and Records

Locked

Lesson 19 of 60 · OOP · Medium

Finish these first:Constructors and Encapsulation →Or pass the tests below to unlock it now.

1

The big idea

enum models a fixed set of constants (can have fields and methods). record is a concise immutable data carrier.

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

enum models a fixed set of constants (can have fields and methods). record is a concise immutable data carrier.

enum Level { LOW, HIGH }
record Point(int x, int y) {
    Point { if (x < 0) throw new IllegalArgumentException(); }
}
3

Quick check

Before writing code: What should `Solution.manhattan(new Solution.Point(0,0), new Solution.Point(3,4))` return when your implementation of Enums and Records is working?

4

Your challenge

2 sample tests, 1 hidden

Define record Point(int x, int y) and static int manhattan(Point a, Point b).

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

Compare with another language

Similar to python.dataclasses-typing (70% overlap). `@dataclass(frozen=True)` ~ Java `record`.

Visual walkthrough

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

Mastery0%

Learn this first

Opens up next

This is the last lesson in this branch.