Java Modifiers and Casting Reference

  • Java Modifiers
  • four possible ways to mix and match superclass references and subclass reference with superclass objects and subclass objects:
    1. referring to a superclass object with a superclass reference is o.k.
    2. referring to a subclass object with a subclass reference is o.k.
    3. referring to a subclassd object with a superclass reference is safe. Such code can only refer to superclass members. If this code refers to subclass-only member through the superclass reference, the compiler will report a syntax error. Instance methods from the superclass that are overridden by the subclass are not accessed when the superclass reference is used, unlike the case for hidden data and hidden static methods. Regardless of the superclass reference used, the subclass's copy is accessed.
    4. referring to a superclass object with a subclass reference is a syntaz error.
  • the references of superclass must be cast in order to access the fields/methods of subclass objects
    Previou page Next page