Can super class reference variable hold sub class's object in java?

Java 8Object Oriented ProgrammingProgramming

Yes, the super class reference variable can hold the sub class object actually, it is widening in case of objects (Conversion of lower datatype to a higher datatype).

But, using this reference you can access the members of super class only, if you try to access the sub class members a compile time error will be generated.

Example

In the following Java example, we have two classes namely Person and Student. The Person class has two instance variables name and age and one instance method displayPerson() which displays the name and age.

The Student extends the person class and in addition to the inherited name and age it has two more variables branch and student_id. It has a method displayData() which displays all four values.

In the main method, we are assigning the subclass object with the super class reference variable

class Person{
   private String name;
   private int age;
   public Person(String name, int age){
      this.name = name;
      this.age = age;
   }
   public void displayPerson() {
      System.out.println("Data of the Person class: ");
      System.out.println("Name: "+this.name);
      System.out.println("Age: "+this.age);
   }
}
public class Student extends Person {
   public String branch;
   public int Student_id;
   public Student(String name, int age, String branch, int Student_id){
      super(name, age);
      this.branch = branch;
      this.Student_id = Student_id;
   }
   public void displayStudent() {
      System.out.println("Data of the Student class: ");
      System.out.println("Name: "+this.name);
      System.out.println("Age: "+this.age);
      System.out.println("Branch: "+this.branch);
      System.out.println("Student ID: "+this.Student_id);
   }
   public static void main(String[] args) {
      Person person = new Student("Krishna", 20, "IT", 1256);
      person.displayPerson();
   }
}

Output

Data of the Person class:
Name: Krishna
Age: 20

Accessing the sub class methods

When you assign the sub class object to the super class reference variable and, using this reference if you try to access the members of the sub class a compile time error will be generated.

Example

In this case, if you assign a Student object to reference variable of Person class as −

Person person = new Student("Krishna", 20, "IT", 1256);

Using this reference, you can access the method of the super class only i.e. displayPerson(). Instead if you try to access the sub class method i.e. displayStudent() a compile time error is generated.

Therefore, if you replace the main method of the previous program with the following a compile time error will be generated.

public static void main(String[] args) {
   Person person = new Student("Krishna", 20, "IT", 1256);
   person.displayStudent();
}

Compile time error

Student.java:33: error: cannot find symbol
   person.dispalyStudent();
          ^
   symbol: method dispalyStudent()
   location: variable person of type Person
1 error
raja
Published on 01-Aug-2019 15:20:51
Advertisements