((Book)item1).display();
Answer:
Because you want to:
- First, cast
item1to typeBook. - Then, invoke the
display()method that objects of typeBookhave.
When is a Type Cast Needed?
Now examine the following:
public static void main ( String[] args )
{
Book book ;
Taxable tax = new Book ( "Emma", 24.95, "Austin" );
book = tax;
book.display();
System.out.println( "Tax on item 1 "+ book.calculateTax() );
}
QUESTION 19:
Is a type cast necessary? Where should it go?