Would the following work?
System.out.println( item1.toString() ); System.out.println( item2.toString() );
Yes, since all objects inherit from Object
so all objects have a toString()
method.
A new method is added to the Book
class:
// Other classes
. . . .
// Book
class Book extends Goods implements Taxable
{
protected String author;
public Book( String des, double pr, String auth)
{
super( des, pr );
author = auth ;
}
public String toString()
{
return super.toString() + "author: " + author ;
}
public double calculateTax()
{
return price * taxRate ;
}
// Newly added method
public String getAuthor()
{
return author;
}
}
public class Store
{
public static void main ( String[] args )
{
Taxable item1 = new Book ( "Emma", 24.95, "Austen" );
System.out.println( "Tax on item 1 "+ item1.calculateTax() );
System.out.println( "Author: " + item1.getAuthor() );
}
}
When a variable is of type Taxable
all the compiler knows is the "taxable" aspect of the object.
The following statement from the above program fails to compile
because Taxable
objects
do not necessarily have the requested method.
System.out.println( "Author: " + item1.getAuthor() );
In a larger program there may be Taxable
objects that do not have getAuthor()
.
The compiler can only use the methods associated by the type of the reference variable.
In the above program, only the calculateTax()
method and methods in Object
are associated with the reference variable.
However, a type cast
tells the compiler that
a particular variable refers to an object of a specific class.
To fix the problem, replace the defective statement with the following.
System.out.println( "Author: " +
((Book)item1).getAuthor() );
Now the getAuthor()
method of the object pointed to by item1
can be found because
the compiler has been told that item1
points to a Book
.
This program is not very sensibly written.
If the variable item1
always points to a Book
it would be more sensible to make it of type Book
.
But programs with complicated logic sometimes need such casts.
Why are the parentheses needed around (Book)item1
?
((Book)item1).getAuthor()