Core Java · Chapter 14 of 48

Inheritance and overriding

By the end of this chapter you can make one class a special kind of another, so it gets everything the other has, and replace just the methods that work differently. 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

Anjali is redoing the menu board for the new kulhad cups: the same chai costs ₹5 more in a clay cup, and the board should say kulhad. Kabir opens Snack.java and finds that someone copied MenuItem into it line by line, then changed the copy. Snacks now print as Bun maska - Rs 50, while everything else says Parle-G: ₹10.

With what you know so far, a chai in a kulhad means a third copy of the menu item code. Every fix to the price or the board line then has to be made in three files, and the copies drift apart, as Snack already has. A method that adds up an order would also need one version per kind of item.

What you build: 5 tasks

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

  1. Task 1

    MenuItem.java: let subclasses read the name and base price

    In MenuItem.java, make the two fields name and basePrice protected instead of private, so the subclasses Chai and Snack can read them. Nothing else changes: a plain menu item prints as before.

    For example, new MenuItem("Parle-G", 10) still prints as Parle-G: ₹10.

  2. Task 2

    Chai.java: a kulhad chai costs ₹5 more

    In Chai.java, add a public method that overrides price() from MenuItem: it returns the base price, plus 5 when the chai is served in a kulhad. It must be declared in Chai itself, with the same name, no parameters and an int result. Java then uses it even when a Chai sits in a MenuItem variable.

    For example, new Chai("Masala chai", 40, true).price() returns 45, new Chai("Masala chai", 40, false).price() returns 40, and a cutting chai at 25 in a kulhad, held in a MenuItem variable, returns 30.

  3. Task 3

    Chai.java: the board line says kulhad

    In Chai.java, override toString() in Chai itself. A chai in a kulhad returns its name, then " (kulhad)", then ": ₹" and its price. A chai in a glass returns the name, ": ₹" and its price, with nothing in between. The price is the one from task 2.

    For example, a masala chai at 40 in a kulhad is Masala chai (kulhad): ₹45, and a cutting chai at 25 in a glass is Cutting chai: ₹25.

  4. Task 4

    Snack.java: make Snack a MenuItem instead of a copy of one

    In Snack.java, make Snack extend MenuItem instead of copying it. Delete its own name and basePrice fields (MenuItem already has them), pass name and basePrice to MenuItem's constructor with super(...), and delete the copied price() and toString(), so a snack uses MenuItem's. Keep the veg field and isVeg().

    For example, new Snack("Bun maska", 50, true) is a MenuItem, prints as Bun maska: ₹50 and has price 50, and new Snack("Egg puff", 30, false).isVeg() is false.

  5. Task 5

    MenuItem.java: add up any mix of menu items

    In MenuItem.java, make the static method total(items) return the sum of the price() of every item it is given, whatever kind of MenuItem each one is: a chai in a kulhad counts with its extra 5 (from task 2). With no items it returns 0.

    For example, total() returns 0, Parle-G at 10 with a masala chai at 40 in a kulhad returns 55, and a cutting chai at 25 in a glass with a bun maska at 50 returns 75.

What you will learn

  • extends: a subclass gets every field and method of its superclass, and adds its own.
  • super(...): a subclass constructor builds the superclass part first.
  • Overriding with @Override: replace one inherited method, and Java picks the version from the object.
  • protected: a field the subclasses may use.

More in Objects

Build "Inheritance and overriding" 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.