![]() |
![]() |
![]() |
![]() |
![]() |
|
On the other hand, because each Java instance
retains all its data, even if it is viewed as an instance of a superclass,
you can cast an Attraction
variable, assigned to a Movie instance, to the Movie class,
and then use any method that can be applied to a Movie instance.
Thus, the following will compile, if the profit method is defined in
both the Movie and Symphony classes.
public class Demonstrate {
public static void main (String argv[]) {
// Movie instance assigned to x:
Attraction x = new Movie (5, 7, 7);
System.out.println("The movie's profit is " + ((Movie)x).profit());
// Symphony instance assigned to x:
x = new Symphony (7, 5, 5);
System.out.println("The symphony's profit is "
+ ((Symphony)x).profit()
);
}
}