Core Java · Chapter 23 of 48

Sealed types and pattern matching

By the end of this chapter you can close a type to a fixed list of kinds, and write a switch that takes each kind apart, checked by the compiler for every case. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Objects
  • 5 tasks
  • Java
  • In the all labs plan

The first 2 chapters of Core Java are free. This one comes with the all labs plan.

Why you need this

Tapri's counters take cash, UPI and cards. Last month another chai company added wallet payments to its till and forgot to teach the refund code about them: wallet refunds silently went nowhere. Meera wants a promise: "If we ever add a fourth way to pay, the program must refuse to build until every part of it knows."

With what you know so far, you would write a chain of instanceof checks, cast each payment, and end with an else that returns something vague. Add a fourth kind and every chain still compiles; the new kind quietly falls into the else. Nothing tells you which code forgot it.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    Payment.java: seal Payment so only cash, UPI and card exist

    In Payment.java, make the Payment interface sealed, permitting exactly Cash, Upi and Card, the three records inside it, and nothing else. The three kinds stay records.

    Do this task first: until Payment is sealed, a switch over the three kinds without a default does not compile, and a compile error fails every task.

  2. Task 2

    Till.java: write the receipt line for any payment

    In Till.java, make receiptLine(payment) return one receipt line, by the kind of payment, using a switch with record patterns and no default. Cash gives "Cash ₹" and the amount. UPI gives "UPI ₹", the amount, " from " and the address. Any card, international or not, gives "Card ₹", the amount, " ending " and the last four digits.

    For example, Cash(100) gives Cash ₹100, Upi("ravi@okaxis", 120) gives UPI ₹120 from ravi@okaxis, and Card("1881", 250, true) gives Card ₹250 ending 1881.

  3. Task 3

    Till.java: charge card fees with a guarded case

    In Till.java, make fee(payment) return the bank's fee in whole rupees, rounded down: cash and UPI cost 0, a card costs 1% of the amount, and an international card 3%.

    For example, Card("4242", 500, false) costs 5, Card("1881", 500, true) costs 15, Card("4242", 199, false) costs 1 and Card("1881", 25, true) costs 0; Cash(500) and a UPI payment of 500 cost 0.

  4. Task 4

    Till.java: explain a refund with nested record patterns

    In Till.java, make refundNote(refund) return what the counter does to pay the refund back, by the kind of payment inside it, with the reason in brackets at the end. Cash gives "Give ₹" and the amount, " back in cash (", the reason and ")". UPI gives "Send ₹", the amount, " to ", the address, " (", the reason and ")". A card gives "Reverse ₹", the amount, " on card ending ", the last four digits, " (", the reason and ")".

    For example, a refund of Cash(40) for cold chai gives Give ₹40 back in cash (cold chai); of Upi("ravi@okaxis", 120) for no stock gives Send ₹120 to ravi@okaxis (no stock); of Card("4242", 500, false) for charged twice gives Reverse ₹500 on card ending 4242 (charged twice).

  5. Task 5

    Till.java: describe anything the old till sends, null included

    In Till.java, make describe(thing) return a description of any object, or of null. Null gives "No payment". A Payment gives its receipt line (task 2) and a Refund its refund note (task 4). A blank String (empty or only spaces) gives "Empty note", any other String gives "Note: " and the text, and anything else gives "Unknown: " and its toString. This needs tasks 2 and 4.

    For example, null gives No payment, "paid at the window" gives Note: paid at the window, three spaces give Empty note, 42 gives Unknown: 42, and a list holding SBC gives Unknown: [SBC].

What you will learn

  • sealed and permits: a type that lists every kind allowed to implement it.
  • Record patterns in a switch: test the kind and take its values out in one case, with no default when the list is closed.
  • Guards with when: a case that matches only under a condition.
  • case null and Object: a switch that also handles null and anything at all.

More in Objects

Build "Sealed types and pattern matching" yourself

Tapri runs chai counters at railway stations, and every counter runs a Java program you write. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Core Java are free. This one comes with the all labs plan.