Core Java · Chapter 17 of 48

Abstract classes

By the end of this chapter you can write a class that is only a skeleton, force every subclass to fill in its missing pieces, and fix the order of steps in one method nobody can change. 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

Every counter writes two reports a day: a handover report when the morning shift ends, and a closing report at night. Ravi has three printouts from three people. One has no total, one puts the total before the items, one forgot to say who closed. He wants the same four steps every time: a title, one line per item, the total cups, a sign-off. Only the title and the sign-off differ.

With chapter 14 you would give a parent ShiftReport a placeholder title(). Nothing makes a subclass replace it: forget, and the report prints the placeholder at the counter. Anyone can also build a plain ShiftReport, a report of no kind, and each subclass can print the steps in its own order.

What you build: 5 tasks

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

  1. Task 1

    ShiftReport.java: make ShiftReport abstract

    In ShiftReport.java, make the ShiftReport class abstract, so it cannot be built with new, and make title() and signOff() abstract methods with no body, still protected and still returning a String. line(item, cups) keeps its default body.

    Careful: once ShiftReport is abstract, MorningReport and ClosingReport compile only when each writes both title() and signOff() (tasks 4 and 5). Until then every task fails with one compile error, so do those tasks first or together with this one.

    For example, a subclass that writes title() and signOff() can be built, while new ShiftReport("SBC", sold) is refused.

  2. Task 2

    ShiftReport.java: count every cup sold in the shift

    In ShiftReport.java, make totalCups() return the cups of every item in the cupsSold map added up. A shift with no sales returns 0.

    For example, Masala chai 42 and Cutting chai 30 return 72; Ginger chai 4 and Bun maska 5 return 9.

  3. Task 3

    ShiftReport.java: write the template method that runs the steps

    In ShiftReport.java, make render() final, so no subclass can override it, and make it return the whole report as one String, its lines joined with a line break and no line break at the end: title(), then line(item, cups) for each item in cupsSold in the map's order, then "Total cups: " and totalCups() (task 2), then signOff(). Get each item's line by calling line(item, cups), because a subclass may change how it looks. With no items there are no item lines.

    For example, a report titled Test report for CSMT that signs off End, with Masala chai 42 and Cutting chai 30, renders five lines: Test report for CSMT, Masala chai: 42, Cutting chai: 30, Total cups: 72 and End. With no items it renders Test report for CSMT, Total cups: 0 and End.

  4. Task 4

    MorningReport.java: fill in the steps for the morning report

    In MorningReport.java, override title() and signOff(), both protected and returning a String. title() returns the station, then " morning shift"; signOff() returns "Handed over to the evening shift". render() (tasks 2 and 3) then prints the morning report.

    For example, for SBC title() is SBC morning shift, and with Masala chai 42 and Cutting chai 30, render() gives five lines: SBC morning shift, Masala chai: 42, Cutting chai: 30, Total cups: 72 and Handed over to the evening shift.

  5. Task 5

    ClosingReport.java: flag slow items and name the cashier

    In ClosingReport.java, override three methods, all protected and returning a String: title() returns the station, then " closing report"; line(item, cups) returns ShiftReport's usual line, then " (slow)" when cups is under 10 (10 is not slow); signOff() returns "Closed by ", then the cashier.

    For example, at NDLS closed by Ravi, line("Ginger chai", 4) is Ginger chai: 4 (slow) and line("Bun maska", 10) is Bun maska: 10. With Masala chai 64, Ginger chai 4 and Bun maska 10, render() gives six lines: NDLS closing report, Masala chai: 64, Ginger chai: 4 (slow), Bun maska: 10, Total cups: 78 and Closed by Ravi.

What you will learn

  • abstract classes: a class that cannot be built with new, only extended.
  • abstract methods: a method with no body, which every concrete subclass must write.
  • The template method: one final method in the parent that runs the steps in a fixed order, calling the subclass's pieces.
  • Hooks and anonymous classes: a step with a default body that a subclass may replace, and a one-off subclass with no name.

More in Objects

Build "Abstract classes" 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.