Yes. By using an interface, the same constant can be used by several classes. This keeps the classes consistent and makes updates easier.
Below is a tiny program that tests the classes.
The calculateTax()
method is only used with objects whose
class implements the interface.
In the picture, clouds represent classes. The dotted rectangle represents the interface. Rectangles represent objects. (Official UML uses different symbols than used here.)
public class StoreTester { public static void main ( String[] args ) { Goods gd = new Goods( "bubble bath", 1.40 ); Food fd = new Food ( "bread", 4.45, 1500 ); Book bk = new Book ( "Emma", 24.95, "Austin" ); Toy ty = new Toy ( "Legos", 54.45, 8 ); System.out.println( gd ); System.out.println( fd ); System.out.println( ty ); System.out.println("Tax is: " + ty.calculateTax() + "\n" ); System.out.println( bk ); System.out.println("Tax is: " + bk.calculateTax() + "\n" ); } }
Has everything been defined? Is there enought to compile and run?