Core Java · Chapter 15 of 48

super, this and final

By the end of this chapter you can build an override on top of the parent's method, lock a method, a class or a variable with final, and hand out a piece of code that runs later. 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

UPI is now most of Tapri's sales, and Anjali wants UPI receipts to show where the money came from, on the receipt's first line. A UpiReceipt subclass is the obvious place. Meera adds a worry of her own: whatever anyone overrides, the GST on a bill must always be worked out the same way, because the auditors will check.

With chapter 14 alone, the UPI header would copy the plain header's text and drift from it the day the plain one changes. And nothing would stop the next subclass from overriding the total with its own GST rule.

What you build: 5 tasks

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

  1. Task 1

    UpiReceipt.java: build the UPI header on the plain one

    In UpiReceipt.java, override header() in UpiReceipt itself (public, returning a String). It returns Receipt's plain header, then " | paid by UPI ", then the UPI id, and it reuses Receipt's version with super.header() instead of copying its text. Receipt's own header stays as it is. Because print() calls header(), a UPI receipt then prints the UPI header as its first line (print also needs total, from task 2).

    For example, new UpiReceipt("NDLS", "priya@okaxis").header() returns Tapri NDLS | paid by UPI priya@okaxis, and its print() starts with that line; new Receipt("SBC").header() is still Tapri SBC.

  2. Task 2

    Receipt.java: lock the GST total with final

    In Receipt.java, make total() final, so no subclass can override it, and make it return the subtotal plus GST_PERCENT (5) percent of it, rounded down to whole rupees with int arithmetic. An empty receipt's total is 0.

    For example, masala chai at 40 and cutting chai at 25 make 65, plus 3 GST (3.25 rounded down), so 68; a subtotal of 155 gives 162. print() of that first receipt is four lines: Tapri SBC, Masala chai ₹40, Cutting chai ₹25 and Total with GST: ₹68.

  3. Task 3

    UpiReceipt.java: seal the class and its UPI id with final

    In UpiReceipt.java, make the class final, so nobody can extend it, and make the upiId field final as well as private, since it never changes once the constructor sets it. getUpiId() keeps returning it.

    For example, new UpiReceipt("SBC", "ravi@okaxis").getUpiId() returns ravi@okaxis.

  4. Task 4

    Receipt.java: a thank-you message that runs later

    In Receipt.java, make thankYou(customer) return a Runnable, never null, that prints one line when it runs: "Thank you, ", the customer, "! Visit Tapri ", the station and " again". Calling thankYou itself prints nothing: the line appears only when the Runnable's run() is called. It works the same on a UpiReceipt.

    For example, running new Receipt("SBC").thankYou("Priya") prints Thank you, Priya! Visit Tapri SBC again and a line break; for a UpiReceipt at CSMT and the customer Ravi it prints Thank you, Ravi! Visit Tapri CSMT again.

  5. Task 5 · interview question

    Interview: Receipt.java, predict what a final list lets you do

    In Receipt.java, make predictOutput() return the lines the code in its comment prints, one string per line, in order, written exactly as Java prints them.

    The check runs the same code and compares your list with what Java really printed.

What you will learn

  • super.method(): call the parent's version from inside an override, and add to it.
  • this versus super: which version a call picks.
  • final: a variable set once, a method nobody overrides, a class nobody extends.
  • Effectively final and lambdas: code you hand out to run later, and the variables it may use.

More in Objects

Build "super, this and final" 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.