Casting
Java is very strict about types
- If a variable is declared to have the type X, you can only invoke operations on it that are defined in class X or its superclasses
- Even though an instance of a subclass of X may be actually stored in the variable
- If you know an instance of a subclass is stored, then you can cast the variable to the subclass
- E.g. if I know a Vector contains instances of String, I can get the next element of its Iterator using:
(String)iterator.next();