Java

What is Downcasting in Java

Upcasting and Downcasting are the types of object-type casting in Java. The parent-to-child type casting is known as Upcasting, and child-to-parent type casting is called Downcasting. Downcasting and Upcasting are the significant features of Java as they allow users to create complex programs with simple syntax.

This write-up will explain what is downcasting in Java.

What is Downcasting in Java?

A type of Object typecasting where the Parent class object is typecasted into the Child class object is known as Downcasting. This functionality is not embedded in Java; we have to use it explicitly. Downcasting is primarily utilized in cases where we want to compare objects. It is also known as Narrowing or Specialization. In Downcasting, all the members of both the parent class and the child classes are easily accessible.


Syntax

The syntax of the Downcasting is:

ChildClass object = (ChildClass)pt;

 
Here, pt is a ParentClass object that is going to be downcasted into the ChildClass object.

To know more about the implementation of Downcasting in Java, check out the below example.

Example

In this example, we will use Downcasting by typecasting the ParentClass object into the ChildClass object. The parent class named ParentClass contains a variable name and a method named displayInfo():

class ParentClass {  
    String name;  
    void displayInfo()  
    {  
        System.out.println("Parent class method");  
    }  
}

 
Whereas, the ChildClass contains an int type variable named rollno and an overridden ParentClass method named displayInfo(). The ChildClass is inherited from the ParentClass by using the extends keyword:

class ChildClass extends ParentClass {  
    int rollno;  
    void displayInfo()  
    {  
        System.out.println("Child class method");  
    }  
}

 
In the main() method, firstly typecast the ChildClass object pt into the ParentClass object through Upcasting ParentClass pt = new ChildClass(). This operation will allow you to access all the variables and member functions of the ParentClass, and for ChildClass you can only access the overridden methods. This is because the ChildClass object now acts like a ParentClass object.

If you want to access the ChildClass members other than overridden methods, downcast the pt object into the ChildClass object as ChildClass cc = (ChildClass)pt. As a result, you can access all the variables and the methods of the both ParentClass and the ChildClass:

public class Casting {
    public static void main(String[] args) {
          ParentClass pt = new ChildClass();  
            pt.name = "John";  
            ChildClass cc = (ChildClass)pt;
            cc.rollno = 15;  
            System.out.println("Name :" + cc.name);  
            System.out.println("Roll# :" + cc.rollno);  
            cc.displayInfo();
    }
}

 

The given output indicates that the downcasted object successfully accessed the properties of both Parent and Child classes:


We represent all the relevant instructions about what is downcasting.

Conclusion

Downcasting is the process in which the Parent Class object is typecasted into the child Class object. It is implemented explicitly in Java. By using Downcasting, you can access the members of both Parent and Child classes. To do so, firstly, Upcast the created Child class object. Then, Downcast it back as a Child Class object. In this writeup, we discussed downcasting in Java in detail.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.