Core Java · Chapter 18 of 48
Interfaces
By the end of this chapter you can describe what a class can do with an interface, let unrelated classes share it, and give the interface methods of its own. 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
Ravi has a new supplier: a bakery at New Delhi station that sends samosas and vada pav, with its own software. Its snacks are nothing like Tapri's chai, but the till must still add them to the bill, and Ravi wants a stock check on anything the counter counts.
A shared parent class cannot do it: the bakery's classes already extend something of their own, and a Java class extends only one class. So the till would need one adding loop for chai, one for samosas, and one more for every new supplier.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Chai.java: report the name, price and stock
In Chai.java, make name(), price() and stock() return the chai's name, price in rupees and cups left, from its three fields. They keep the promises Sellable and Stocked make, so they work through a Sellable or a Stocked variable too.
For example, new Chai("Masala chai", 40, 12) used as a Sellable has the name Masala chai and the price 40; new Chai("Cutting chai", 25, 30) used as a Stocked has the stock 30.
- Task 2
Sellable.java: give every Sellable a label with a default method
In Sellable.java, make the default method label() return the name, then ": ₹", then the price. It must stay a default method in the interface, so any class that implements Sellable gets it, even one that only writes name() and price().
For example, a snack Bun maska at 50 gives Bun maska: ₹50, and Parle-G at 10 gives Parle-G: ₹10.
- Task 3
Sellable.java: add up any list of sellables with a static method
In Sellable.java, make the static method total(items) return the price() of every item in the list added up, in rupees. The items can be any class that implements Sellable, not only Chai. An empty list returns 0.
For example, Bun maska 50, Samosa 15 and Vada 25 return 90.
- Task 4
Stocked.java and Chai.java: check and sell stock honestly
In Stocked.java, make the default method canSell(cups) return true when cups is at least 1 and no more than stock(); 0 and negative numbers are false. In Chai.java, make sell(cups) take cups off the stock, but when canSell(cups) is false, throw an IllegalArgumentException and leave the stock as it was. The check reads the stock with stock(), from task 1.
For example, something with 5 left (even a lambda) can sell 5 but not 6, 0 or -1. A Masala chai with 3 left has 1 left after sell(2); a second sell(2) throws, and 1 is still left.
- Task 5 · interview question
Interview: Chai.java, resolve two default methods with the same name
In Chai.java, make describe() return Sellable's default description, then ", ", then Stocked's default description. Reuse both defaults instead of writing their text again. Both call name(), price() and stock(), so this needs task 1.
For example, new Chai("Masala chai", 40, 12) gives Masala chai at ₹40, 12 in stock, and Cutting chai at 25 with 0 left gives Cutting chai at ₹25, 0 in stock.
What you will learn
- interface and implements: methods a class promises to have, and a class keeping the promise (a class can keep many).
- The interface as a type: a variable or a list that accepts any class keeping the promise.
- default and static methods: code that lives in the interface itself.
- Two defaults with one name: why Java refuses to guess, and how you choose with Interface.super.
More in Objects
Build "Interfaces" 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.