Open In App

Referencing Subclass objects with Subclass vs Superclass reference

Prerequisite : Inheritance
In Java, all non-static methods are based on the runtime type of the underlying object rather than the type of the reference that points to that object. Therefore, it doesn’t matter which type you use in the declaration of the object, the behavior will be the same.

How to Refer a subclass object



There are two approaches to refer a subclass object. Both have some advantages/disadvantages over the other. The declaration affect is seen on methods that are visible at compile-time.

  1. First approach (Referencing using Superclass reference): A reference variable of a superclass can be used to a refer any subclass object derived from that superclass. If the methods are present in SuperClass, but overridden by SubClass, it will be the overridden method that will be executed.
  2. Second approach (Referencing using subclass reference) : A subclass reference can be used to refer its object.

Consider an example explaining both the approaches.




// Java program to illustrate 
// referring to a subclass
// base class
class Bicycle 
{
    // the Bicycle class has two fields
    public int gear;
    public int speed;
          
    // the Bicycle class has one constructor
    public Bicycle(int gear, int speed)
    {
        this.gear = gear;
        this.speed = speed;
    }
          
    // the Bicycle class has three methods
    public void applyBrake(int decrement)
    {
        speed -= decrement;
    }
          
    public void speedUp(int increment)
    {
        speed += increment;
    }
      
    // toString() method to print info of Bicycle
    public String toString() 
    {
        return("No of gears are "+gear
                +"\n"
                + "speed of bicycle is "+speed);
    
}
  
// derived class
class MountainBike extends Bicycle 
{
      
    // the MountainBike subclass adds one more field
    public int seatHeight;
  
    // the MountainBike subclass has one constructor
    public MountainBike(int gear,int speed,
                        int startHeight)
    {
        // invoking base-class(Bicycle) constructor
        super(gear, speed);
        seatHeight = startHeight;
    
          
    // the MountainBike subclass adds one more method
    public void setHeight(int newValue)
    {
        seatHeight = newValue;
    
      
    // overriding toString() method
    // of Bicycle to print more info
    @Override
    public String toString() 
    {
          
        return (super.toString()+
                "\nseat height is "+seatHeight);
    }
      
}
  
// driver class
public class Test 
{
    public static void main(String args[]) 
    {
        // using superclass reference
        // first approach
        Bicycle mb2 = new MountainBike(4, 200, 20);
          
        // using subclass reference( )
        // second approach
        MountainBike mb1 = new MountainBike(3, 100, 25);
          
        System.out.println("seat height of first bicycle is " 
                                            + mb1.seatHeight);
              
        // In case of overridden methods
        // always subclass 
        // method will be executed
        System.out.println(mb1.toString());
        System.out.println(mb2.toString());
  
        /* The following statement is invalid because Bicycle
        does not define a seatHeight. 
        // System.out.println("seat height of second bicycle is " 
                                                + mb2.seatHeight); */
                      
        /* The following statement is invalid because Bicycle
        does not define setHeight() method. 
        mb2.setHeight(21);*/
  
    }
}

Output:



seat height of first bicycle is 25
No of gears are 3
speed of bicycle is 100
seat height is 25
No of gears are 4
speed of bicycle is 200
seat height is 20

Explanation of above program :

When to go for first approach (Referencing using superclass reference)

If we don’t know exact runtime type of an object, then we should use this approach. For example, consider an ArrayList containing different objects at different indices. Now when we try to get elements of arraylist using ArrayList.get(int index) method then we must use Object reference, as in this case, we don’t know exact runtime type of an object. For example :




/* Java program to illustrate referring to a subclass
using superclass reference variable */
import java.util.ArrayList;
  
public class Test 
{
       public static void main(String args[]) 
       {
           ArrayList al = new ArrayList(2);
             
           // adding String object to al
           al.add(new String("GeeksForGeeks"));
             
           // adding Integer object to al
           al.add(new Integer(5));
             
           // getting all elements using Object reference
           for (Object object : al)
           {
               System.out.println(object);
           }    
       }
}

Output:

GeeksForGeeks
5

Advantage : We can use superclass reference to hold any subclass object derived from it.

Disadvantage : By using superclass reference, we will have access only to those parts(methods and variables) of the object defined by the superclass. For example, we can not access seatHeight variable or call setHeight(int newValue) method using Bicycle reference in above first example. This is because they are defined in subclass not in the superclass.

When to go for second approach (Referencing using subclass reference)

If we know the exact runtime type of an object, then this approach is better. Using this approach, we can also call a particular object specific methods. For example :




/* Java program to illustrate referring to a subclass
using subclass reference variable */
import java.util.ArrayList;
  
public class Test 
{
       public static void main(String args[]) 
       {
           ArrayList al = new ArrayList(2);
             
           // adding String objects to al
           al.add(new String("GeeksForGeeks"));
           al.add(new String("for java archives"));
             
              
           // getting  elements using String reference
            String str1 = (String)al.get(0);
            String str2 = (String)al.get(1);
              
            System.out.println(str1);
            System.out.println(str2);
              
            // using String class specific method
            System.out.println(str1.length());
            System.out.println(str2.substring(4,8));
       }
}

Output:

GeeksForGeeks
for java archives
13
java

Advantage : By using subclass reference, we will have access to both parts(methods and variables) of the object defined by the superclass or subclass. For example, we can call setHeight(int newValue) method or speedUp(int increment) method using MountainBike reference in above first example.

Disadvantage : We can use subclass reference to hold only for that particular subclass objects only.

Reference: StackOverflow


Article Tags :