Home Segments Top Top Previous Next

365: Mainline

On the other hand, because each Java instance retains all its data, even if the instance is viewed as an implementer of an interface, you can cast a RatingInterface variable, bound 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: 
  RatingInterface 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() 
                    ); 
 } 
}