Java Reference
In-Depth Information
Self-Test Exercises
(continued)
8 .
★
What output is produced by the following code?
Sale someObject =
new
Sale("map", 5);
DiscountSale ds =
new
DiscountSale();
if
(someObject
instanceof
DiscountSale)
{
ds = (DiscountSale)someObject;
System.out.println("ds was changed to " + someObject);
}
else
System.out.println("ds was not changed.");
★
9.
Suppose we removed the qualifi er
static
from the method
announcement()
in both
Sale
( Display 8.1 ) and
DiscountSale
( Display 8.2 ). What would
be the output produced by the following code (which is similar to the end of
Display 8.4 )?
Sale s =
new
Sale( );
DiscountSale discount =
new
DiscountSale( );
s.announcement( );
discount.announcement( );
System.out.println("No surprises so far, but wait.");
Sale discount2 = discount;
System.out.println(
"discount2 is a DiscountSale object in a Sale variable.");
System.out.println(
"Which definition of announcement( ) will it use?");
discount2.announcement( );
System.out.println(
"Did it use the Sale version of announcement()?");