|
Would the following work? item1.display() ; A good answer might be:No---all the compiler has been told is that item1 implements the methods in the interface Taxable. The display() method is not in the interface. | |
Type CastsWhen you use a variable of type Taxable you are asking to use the "taxable" aspect of the object. Many different kinds of objects might be referred to by the variable. (In a larger program there may be Taxable classes that are not Goods.) The compiler can only use the methods it knows that the object must have---those in the interface However, you can use a type cast to tell the compiler that in a particular statement in the program the variable will refer to an object of a specific class:
This program is not very sensibly written, since if the variable item1 were of type Book everything would work without the need for a type cast. But in programs with more complicated logic such casts are sometimes needed. | |
QUESTION 16:Why are the red parentheses needed in:
Click Here after you have answered the question
|