|
|
Rule: a reference variable must reference to an object that contains all members defined in its class
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 type:
However: there is another safe 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 Overriding some methods:
An instance variable a of the type SomeClass can be used to access the (public) members in SomeClass:
Rule: a reference variable must reference to an object that contains all members defined in its class
public class myProg
{
public static void main(String[] args)
{
// The normal way to
// satisfy the above rule:
SomeClass a = new SomeClass();
// Make a reference to an object
// of the same class
System.out.println(a.x);
a.method1();
a.method2();
}
}
|
public class SomeClass
{
public int x = 44;
public void method1()
{
System.out.println("SomeClass.m1()");
}
public void method2()
{
System.out.println("SomeClass.m2()");
}
}
|
An instance variable a of the type NewClass can be used to access the (public) members in NewClass :
Rule: a reference variable must reference to an object that contains all members defined in its class
public class myProg
{
public static void main(String[] args)
{
// The normal way to
// satisfy the above rule:
NewClass a = new NewClass();
// Make a reference to an object
// of the same class
System.out.println(a.x);
a.method1();
a.method2();
a.method3();
}
}
|
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()");
}
}
|
Notice: a subclass object can perform more actions that a superclass object performs:
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();
}
}
|
Notice: a subclass object can perform all actions that a superclass object performs:
Therefore, it is safe to use a superclass reference variable to access 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
}
}
|
public class myProg
{
public static void main(String[] args)
{
// Normal way to access member in NewClass
NewClass a = new NewClass();
System.out.println(a.x);
a.method1();
a.method2();
a.method3();
// New way to access member in NewClass
SomeClass b = new NewClass(); // Allowed !
System.out.println(b.x);
b.method1();
b.method2();
// b.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()");
}
}
|
A superclass reference variable is allowed to reference to a subclass object !
|