Yes.
An interface can be used as a data type for a reference variable.
Since Toy
and Book
implement Taxable
,
they can both be used with a reference variable of type Taxable
:
public static void main ( String[] args ) { Taxable item1 = new Book ( "Emma", 24.95, "Austin" ); Taxable item2 = new Toy ( "Leggos", 54.45, 8 ); System.out.println( "Tax on item 1 "+ item1.calculateTax() ); System.out.println( "Tax on item 2 "+ item2.calculateTax() ); }
The compiler has been told in the interface that all Taxable
objects will have a calculateTax()
method,
so that method can be used with the variables.
Would the following work?
item1.display() ;