Home Segments Top Top Previous Next

569: Mainline

As you learned in Segment 342, you can cast an instance to a class, as long as the instance is, in fact, an instance of the class.

For example, you can work with an element of a vector by casting that element, an Object instance, into a Movie instance:

  *-- Cast to the Movie class                   | 
  |        *-- Array access                     | No bug!  
  |        |                   *-- Method call  | Movie instance has 
  v        v                   v                | rating method 
 ------- ------------------- --------           | 
((Movie) (v.firstElement())).rating()           |