Eine Typumwandlung ist notwendig, um dem Compiler mitzuteilen, dass die Variable besteuerbar in diesem Fall eine Referenz auf ein Book-Objekt enthält:
public class KaufhausTester3
{
public static void main(String[] args)
{
Book buch ;
Taxable besteuerbar =
new Book("Emma", 24.95, "Austin");
buch = (Book)besteuerbar;
System.out.println(buch);
System.out.println("Steuer Artikel 1 " +
buch.calculateTax();
}
}
Wir betrachten jetzt den folgenden Code:
public class StoreTester4
{
public static void main(String[] args)
{
Goods toy ;
Taxable tax =
new Toy("Building Blocks", 1.49, 6);
toy = tax;
System.out.println(toy);
System.out.println("Tax: " +
toy.calculateTax());
}
}
Sind Typumwandlungen notwendig? Wo sollten sie vorgenommen werden?
Hinweis: Es gibt mehrere korrekte Antworten.