A type cast is necessary to tell the compiler that the variable tax
in this case contains a Book
object:
public static void main ( String[] args )
{
Book book ;
Taxable tax = new Book ( "Emma", 24.95, "Austin" );
book = (Book)tax;
book.display();
System.out.println( "Tax on item 1 "+ book.calculateTax() );
}
Now Consider the following code:
public static void main ( String[] args ) { Goods toy ; Taxable tax = new Toy ( "Building Blocks", 1.49, 6 ); toy = tax; toy.display(); System.out.println( "Tax: "+ toy.calculateTax() ); }
Are type casts necessary? Where should they go? Hint: there are several correct answers.