What happens when a subclass object is assigned to a superclass object in Java?


Converting one data type to others in Java is known as casting.

  • If you convert a higher datatype to lower datatype, it is known as narrowing (assigning higher data type value to the lower data type variable).

char ch = (char)5;
  • If you convert a lower data type to a higher data type, it is known as widening (assigning lower data type value to the higher data type variable).

Int i = 'c';

Similarly, you can also cast/convert an object of one class type to others. But these two classes should be in an inheritance relation. Then,

  • If you convert a Super class to subclass type it is known as narrowing in terms of references (Subclass reference variable holding an object of the superclass).

Sub sub = (Sub)new Super();
  • If you convert a Sub class to Super class type it is known as widening in terms of references (super class reference variable holding an object of the sub class).

Super sup = new Sub();

Assigning subclass object to a superclass variable

Therefore, if you assign an object of the subclass to the reference variable of the superclass then the subclass object is converted into the type of superclass and this process is termed as widening (in terms of references).

But, using this reference you can access the members of superclass only if you try to access the subclass 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 display person() 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 display data() which displays all four values.

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

 Live Demo

class Person{
   public String name;
   public 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: "+super.name);
      System.out.println("Age: "+super.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 subclass methods

When you assign the subclass object to the superclass reference variable and, using this reference if you try to access the members of the subclass 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 superclass only i.e. display person(). Instead, if you try to access the subclass method i.e. display student() 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

Updated on: 10-Sep-2019

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements