Referencing Subclass objects with Subclass vs Superclass reference


In java inheritance some of the basic rules includes −

  • Object relation of Superclass (parent) to Subclass (child) exists while child to parent object relation never exists.This means that reference of parent class could hold the child object while child reference could not hold the parent object.

  • In case of overriding of non static method the runtime object would evaluate that which method would be executed of subclass or of superclass.While execution of static method depends on the type of reference that object holds.

  • Other basic rule of inheritance is related to static and non static method overriding that static method in java could not be overridden while non static method can be.However subclass can define static method of same static method signature as superclass have hut that would not be consider as overriding while known as hiding of static method of superclass.

On the basis of above rules we would observe different scenarios with the help of program.

Example

 Live Demo

class Parent {
   public void parentPrint() {
      System.out.println("parent print called");
   }
   public static void staticMethod() {
      System.out.println("parent static method called");
   }
}
public class SubclassReference extends Parent {
   public void parentPrint() {
      System.out.println("child print called");
   }
   public static void staticMethod() {
      System.out.println("child static method called");
   }
   public static void main(String[] args) {
      //SubclassReference invalid = new Parent();//Type mismatch: cannot convert from Parent to          SubclassReference
      Parent obj = new SubclassReference();
      obj.parentPrint(); //method of subclass would execute as subclass object at runtime.
      obj.staticMethod(); //method of superclass would execute as reference of superclass.
      Parent obj1 = new Parent();
      obj1.parentPrint(); //method of superclass would execute as superclass object at runtime.
      obj1.staticMethod(); //method of superclass would execute as reference of superclass.
      SubclassReference obj3 = new SubclassReference();
      obj3.parentPrint(); //method of subclass would execute as subclass object at runtime.
      obj3.staticMethod(); //method of subclass would execute as reference of subclass.
   }
}

Output

child print called
parent static method called
parent print called
parent static method called
child print called
child static method called

So difference between referencing using superclass reference and referencing using subclass reference is use superclass referencing can holds object of subclass and could only access the methods which are defined/overridden by subclass while use subclass referencing can not hold object of superclass and could access the methods of both superclass and subclass.

Updated on: 25-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements