Core Java · Chapter 19 of 48

Polymorphism

By the end of this chapter you can treat many kinds of object through one parent type, let each one answer in its own way, and check an object's real class safely when you must. 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

At the 6 am rush in Bengaluru a customer asks for the morning combo with the chai in a clay kulhad. The counter taps Upgrade, and the till crashes: the old upgrade code assumed every item was a chai. Ravi has a second problem: he never knows how many kulhads to have ready, because some chai is hidden inside combos.

With what you know so far, the till would ask every item what it is, with one branch per class, to work out its price. Every new kind of item means editing that code, and a forgotten branch charges the wrong price. And when the till does need a chai, a blind cast crashes on a bun maska.

What you build: 5 tasks

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

  1. Task 1

    Chai.java and Combo.java: override price

    In Chai.java, make price() return MenuItem's price plus 5 when the chai is served in a kulhad, and MenuItem's price when it is not. In Combo.java, make price() return the price() of every item in the combo added up, minus 10. An item in a combo can be a kulhad chai or another combo, and each answers with its own price().

    For example, Masala chai at 40 in a kulhad costs 45, even through a MenuItem variable, and Cutting chai at 25 in a paper cup costs 25. That kulhad chai with a Bun maska at 50 is a combo at 85 (45 + 50 - 10), and that combo with two paper-cup Cutting chai is a combo at 125 (85 + 25 + 25 - 10).

  2. Task 2

    Till.java: total an order of any menu items

    In Till.java, make total(order) return the price() of every item in the order added up, in rupees. Ask each item for its own price, because the order can hold subclasses of MenuItem the till has never heard of. An empty order returns 0.

    For example, Bun maska at 50, a station special whose own price() returns 99 (though its base price is 0) and Parle-G at 10 return 159.

  3. Task 3

    Till.java: count the kulhads an order needs, inside combos too

    In Till.java, make kulhadsNeeded(order) return how many kulhads Ravi must have ready: one for every Chai served in a kulhad, including the chai inside combos, and inside a combo inside a combo. Paper-cup chai and other items need none. An order with none returns 0.

    For example, a bun and a paper-cup chai need 0; a kulhad chai, a paper-cup chai and a combo holding a kulhad chai and a bun need 2; a combo holding (a combo of two kulhad chai), a kulhad chai and a paper-cup chai needs 3.

  4. Task 4

    Till.java: upgrade chai to a kulhad without a ClassCastException

    In Till.java, make upgradeToKulhad(item) return, for a Chai, a new Chai with the same name and base price, served in a kulhad. Any other item, a combo too, comes back unchanged: the very same object. It never throws.

    For example, a paper-cup Masala chai at 40 comes back as a Chai named Masala chai, base price 40, in a kulhad; a Bun maska comes back as that same Bun maska object.

  5. Task 5 · interview question

    Interview: Till.java, predict which overload and which override run

    In Till.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

  • Upcasting and dynamic dispatch: a parent-type variable holds any subclass, and each object runs its own override.
  • Declared type against real class: the compiler checks calls against the variable's type; the JVM picks the body from the object.
  • Downcasting and ClassCastException: a cast that is checked when it runs, and how it fails.
  • instanceof with a pattern: ask first, and get a typed variable in the same step.

More in Objects

Build "Polymorphism" 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.