|
|
|
Suppose a SomeClass variable a is referencing to some arbitrary object:
We can make these legal member access requests: (1) a.x (2) a.method1() (3) a.method2()
Rule: a reference variable must reference to an object that contains all members in a legal request
Reason: the referenced object must perform all actions in the class or else we can have an request error !
The simplest way to satisfy the rule is to reference to an object of the same class:
However: there is another safe (= correct) way due to the inheritance relationship !!
A subclass inherits all the normal members (excluding constructors) from its superclass:
A subclass object contains all normal members found in a superclass object:
A subclass object contains all normal members even when we override some methods:
An instance variable a of the type SomeClass can be used to access the (public) members in SomeClass:
An instance variable a of the type NewClass can be used to access the (public) members in NewClass :
Notice that: a subclass object can perform all actions that a superclass object performs:
Therefore, it is safe to use a superclass reference variable to request members in a subclass object:
Java does allow you to access members in a subclass object using a superclass reference variable !
public class myProg { public static void main(String[] args) { SomeClass a; // Defines a reference variable of // type SomeClass // Allowable actions: // a.x // a.method1(); // a.method2(); a = new NewClass(); // Make a reference to a NewClass object // Legal, because the NewClass object // can perform all actions // required by the SomeClass type } } |
DEMO: demo/04-inheritance/18-casting/Demo4.java
Furthermore: the request a.method() will execute the method in the object that a is currently pointing to:
This feature is called "dynamic dispatch" or late binding (decision on which method to run is at the last moment)
public class myProg { public static void main(String[] args) { // New way to access member in NewClass SomeClass a = new NewClass(); // Allowed ! System.out.println(a.x); a.method1(); // exec NewClass' method1() a.method2(); // a.method3(); // This is illegal ! } } |
public class NewClass extends SomeClass
{
public void method1() // Override
{
System.out.println("**NewClass.m1()");
}
public void method3()
{
System.out.println("**NewClass.m3()");
}
}
|
public class SomeClass { public int x = 44; public void method1() { System.out.println("SomeClass.m1()"); } public void method2() { System.out.println("SomeClass.m2()"); } } |
DEMO: demo/04-inheritance/18-casting/Demo.java
Because a is pointing to a NewClass object, a.method1() will execute NewClass' method1() eventhough a is a SomeClass variable
|
DEMO: demo/04-inheritance/18-casting/Demo2.java
Notice: a subclass object may perform fewer actions that a subclass object can perform:
Therefore, it is illegal to use a subclass reference variable to access members in a superclass object:
E.g.: the action a.method3() is illegal because there is no method3() defined inside a SomeClass object
public class myProg { public static void main(String[] args) { NewClass a; // Defines a reference variable of // type NewClass // Allowable actions: // a.x // a.method1(); // a.method2(); // a.method3(); a = new SomeClass(); // Make a reference to a SomeClass object // Illegal, because the SomeClass object // cannot perform: // a.method3(); } } |
DEMO: demo/04-inheritance/18-casting/Demo3.java