An interface as a parameter's type
A watch shop sells products, such as straps and alarm clocks, and it does repairs, which cost 2 dollars a minute. A product and a repair have little in common: a product has a name and a fixed price, and a repair has minutes of work. But at the checkout, both need a line on the receipt and a price. That's an interface:
public interface Priced {
String label();
int price();
}
A method whose parameter has the type Priced accepts both:
public class Main {
public static void main(String[] args) {
Product strap = new Product("Watch strap", 12);
Repair repair = new Repair(25);
printLine(strap);
printLine(repair);
printLine(new Product("Alarm clock", 45));
}
public static void printLine(Priced item) {
System.out.println(item.label() + ": " + item.price() + " dollars");
}
}public interface Priced {
String label();
int price();
}public class Product implements Priced {
private String name;
private int dollars;
public Product(String name, int dollars) {
this.name = name;
this.dollars = dollars;
}
@Override
public String label() {
return this.name;
}
@Override
public int price() {
return this.dollars;
}
}public class Repair implements Priced {
private int minutes;
public Repair(int minutes) {
this.minutes = minutes;
}
@Override
public String label() {
return "Repair, " + this.minutes + " minutes";
}
@Override
public int price() {
return 2 * this.minutes;
}
}Watch strap: 12 dollars
Repair, 25 minutes: 50 dollars
Alarm clock: 45 dollarsprintLine's parameter has the type Priced, so the method accepts an object of any class that implements Priced. The callprintLine(strap)works as if it saidPriced item = strap;, which compiles because a Product is a Priced.- Inside the method,
itemis a Priced variable, like step 3'sdevice. The method may calllabel()andprice(), and each object runs its own: the product gives its fixed price, the repair works its price out from the minutes.
One method for every kind of item
Without the interface, printLine would need two versions, printLine(Product item) and printLine(Repair item), with the same code in each (overloading, module 21). Every new thing the shop sells would need one more.
With the interface, printLine is written once, and it depends only on what it uses: a label and a price. When the shop starts engraving watches, a new class Engraving that implements Priced works with printLine at once, without any change to it. The last challenge of this step shows that.
Java's own classes work this way too. System.out.println can print any object because it has a version whose parameter's type is Object, and step 6 shows that Collections.sort takes an interface.