More Practice

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.

Answer:

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() );
  }

More Practice

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() );
  }

QUESTION 20:

Are type casts necessary? Where should they go? Hint: there are several correct answers.