Here is one correct answer:
public static void main ( String[] args ) { Goods toy ; Taxable tax = new Toy ( "Grobot", 1.49, 6 ); toy = (Toy)tax; toy.display(); System.out.println( "Tax: "+ ((Taxable)toy).calculateTax() ); }
The first type cast must cast tax
to a type
Goods
(or an ancestor).
The second must cast toy
to Taxable
or to
a class that implements Taxable
.
Here is another possible answer:
public static void main ( String[] args ) { Goods toy ; Taxable tax = new Toy ( "Grobot", 1.49, 6 ); toy = (Goods)tax; toy.display(); System.out.println( "Tax: "+ ((Toy)toy).calculateTax() ); }