Inheritance and References
Home - About Us
Inheritance and References Example 1 Example 2 Exnample 3

References to Superclasses

When an object is created from a class that has a superclass (which is true for every class except the Object class) then you have the option of creating a reference to the entire object, or you can create a reference of the type of the super class.

For example, if we have defined the class A:

class A
{
   int a = 3;
   
   void inA()
   {
      System.out.println("in A");
   }
}

And create a variable refA to hold a reference to an A object:

A refA = new A();

Then we have the structure illustrated in the picture below:

Object A with reference

If we define a class B as follows:

class B extends A
{
   int b = 7;
   
   void inB()
   {
      System.out.println("in B");
   }
}

and create a B object and a reference of type B:

B refB = new B();

then we have a the object as illustrated below:

Object B with refB reference

Note that the A object is at the core of the B object. The reference refB can be used to reference all members of the B object, including all members of the inherited A object:

int val1 = refB.a;  // The value 3 is retrieved here
int val2 = refB.b;  // The value 7 is retrieved

refB.inA();         // Prints "in A" to the console
refB.inB();         // Prints "in B" to the console

The reference refB:

  • Can access any of the methods or data members in class A or B (if the A methods are not defined as private).
  • If a method shows up in both A and B, and the two methods have identical signatures, then the outermost method is accessed (the outermost method being the one farthest from the superclass -- in this example the outermost method is the one defined in the B class).

Referencing the Superclass Object

If a reference a of type A is declared and made to reference a B object:

	A a2 = (A) new B();

Then the reference a2:

  • Can only access data members and methods defined in the A class.

This is illustrated in the following picture:

B object with A reference

Some of the statements in the following code are illegal:

int x = a2.a;
int y = a2.b;  // NOT ALLOWED - this line will not compile

a2.inA();      // Prints "in A" to the console
a2.inB();      // NOT ALLOWED - this line will not compile

Polymorphism

Things get more interesting for an A reference to a B object if a method with the same signature appear in both the A and the B class:

  • If a method defined in the A class is overridden in the B class (i.e., the method appears again in the B class and the two methods have identical signatures), then method in the B class is executed, even if you explicitly attempt to invoke the A instance of the method -- this is polymorphism.

The following code puts a method "where" in both the A and the B classes:

class A
{
   int a = 3;
   
   void inA()
   {
      System.out.println("in A");
   }
   
   void where()
   {
      System.out.println("inside");
   }
}

class B extends A
{
   int b = 7;
   
   void inB()
   {
      System.out.println("in B");
   }
   
   void where()
   {
      System.out.println("outside");
   }
}

The following code creates a reference a3 which is then used to call the "where" method:

A a3 = (A) new B();
a3.where();           // Prints "outside" to the console!

The call to "where" is legal because the "where" method is defined in the class A. However, an improved version of the "where" method exists in the subclass B -- Java jumps out to this overridden version of "where" and executes that method. Note that this polymorphic behavior only works for methods, but not for data members.

Polymorphism is shown here

Home - About Us
Copyright © 2006 by Kiowok, Ann Arbor, Michigan, USA