Upcast And Downcast Basics

In this blog, we will learn about Downcast and Upcast with the help of a simple example.
 
Let us first learn what Downcast is. Downcast, as the name suggests, is the process of going down from the top. Yes, it means going from top to bottom, i.e, from Super class to Sub class.

Now, let us come to an example in order to understand it better. The very basic rule in casting that we should never forget is that - "A child class can always refer to its parent class but the parent class can't refer to its child class."

Example

Suppose, we have 3 classes - Parent, Child, and another child (Sibling) in such a way that Child extends Parent and Child's child (sibling) extends Child.

Case1
  1. Parent parent = new Child(); //Assigning child object to parent ref. variable  
  2. Child child = (Child) parent; //Down cast parent ref. variable to child.  
  3. child.sleep(); //Calling child class sleep method and it will work fine.   

Now, 

  1. Sibling sibling = (Sibling) child //DownCast child ref. variable to sibling  
  2. sibling.cry(); //Class cast exception will occur as we are trying to do here is we are moving parent class object(Child object) into child class(Sibling) which can never happen because child object can refer to parent but parent object can never refer to child.   
Which means something like this,

  1. Child child = new Parent(); //This can never be possible as parent object can never refer to child.  

Use Case2 

  1. Parent parent = new Sibling(); //Assigning Sibling object to parent ref. variable  
  2. Child child = (Child) parent; // Down cast parent ref. variable to child.  
  3. Sibling sibling = (Sibling) child; // Down cast sibling ref. variable to sibling.  
  4. sibling.walk(); //Calling sibling class walk method and it will run perfectly.   
This will work because we have cast parent to child and then child to sibling.

Use Case3 

  1. Parent parent = new Child(); //Assigning child object to parent ref. variable.  
  2. parent = new Sibling(); // Assigning sibling object to parent.  
  3. Child child = (Child) parent; // Down cast parent ref. variable to child  
  4. child.sleep(); // Calling sleep method of Child class and it will run perfectly as child object here (Sibling) can refer to Child class(parent)   
Upcast

We need to remember that in this type of casting, we need not explicitly cast the object as we are going upwards to the parent class. So, the access to the number of methods will be restricted automatically.

Example 
  1. Child child = new Child();  
  2. Parent parent = (Parent) child; //Upcasting with an explicit cast  
  3. Parent parent = child; // Upcasting with no explicit cast.  
  4. parent.work(); // Parent class work method will run. Because we know that a child can always refer to parent class.   

But if we try to invoke child class method here, we will get an error because at compile time, it will check the reference variable which is of type parent but the method we are trying to call is of child class.

So, the compiler will check parent class and won’t find that method. Then, it will throw error.

  1. parent.sleep(); //Error at compile time as sleep is child class method.  
Ebook Download
View all
Learn
View all