Sellable.java
Download Sellable.java
1: package com.wpollock.iface;
2:
3: /**
4: * The Sellable interface from the GiftBasket demo has two methods,
5: * cost and describe; here, we add a third method to that interface.
6: *
7: * To prevent old code such as GiftBasket.java from breaking when we
8: * add methods to interfaces it uses, we must also add a "default"
9: * implementation of this method.
10: *
11: * @author Wayne Pollock, Tampa Florida USA
12: *
13: */
14: public interface Sellable {
15:
16: /**
17: * Compute the cost of a Sellable item
18: * @return the price of a Sellable item
19: */
20: float cost();
21:
22: /**
23: * Print to System.out a description of a Sellable item.
24: * (This is a bad design; the method should return a String instead.)
25: */
26: void describe ();
27:
28: /**
29: * Sellable items may have tax computed. This is the default rate.
30: * (In an interface, all variables are automatically public, static,
31: * and final.)
32: */
33: double DEFAULT_TAX_RATE = 0.07;
34:
35: /**
36: * Compute the tax to be collected on a Sellable item. Not all such
37: * items have tax. Since this is a new method for an existing
38: * interface, we need to provide a default implementation.
39: *
40: * @return The tax to be collected, in U.S. dollars.
41: */
42: default double tax() {
43: return DEFAULT_TAX_RATE * cost();
44: }
45:
46: /*
47: * Interfaces can have nested classes or other interfaces, not shown here.
48: * Additionally, interfaces can contain static methods; these are
49: * useful for related "utility" methods that otherwise would need to be
50: * put in some other class.
51: */
52: }