Upcasting and DownCasting In Java

Upcasting and DownCasting In Java:-

It is one of the most popular interview questions in Java. We will talk about  Up casting and Down Casting in Java. Let’s have a look at the problem or rather question.

class Animal
{ 
    public void callme()
    {
      System.out.println("Called Human");
    }
}


class Man extends Animal
{ 
    
    public void callme2()
    {
        System.out.println("Called Man Call2");
    }
}

public class Test
{
    public static void main (String [] args) 
    {
       Animal animal = new Man(); /---- Point 1
       Man man=new Animal(); /--- point 2
       man.callme2()
    }
}

What would be the Output ? If you know the answer then i think you are not the target audience here and you can skip this post.

Answer:-

For Point 2 there will be a compilation Error as well as runtime error.

Let’s discuss why ?

What is Upcasting & Downcasting Here:-

Upcasting is casting to a supertype, while downcasting is casting to a subtype. Upcasting is always allowed, but downcasting involves a type check and can throw a ClassCastException.

In your case, a cast from Man to an Animal is an upcast, because of a Man is-a Animal In general, you can upcast whenever there is an is-a relationship between two classes.

We will now resolve both the  Issues.

1. Bypass Compilation Issue by Casting:-

So the Code Looks Like:-

        Animal animal = new Man();
        Man man=(Man) new Animal();
        man.callme2();

Here you Will get a ClassCastException The reason why is because animal‘s runtime type is,Animal .

Now at runtime, it performs the cast and it seems that animal isn’t really a Man and so throws a ClassCastException .

We can use the instanceof operator to check the runtime type of the object before performing the cast, which allows you to prevent ClassCastException.

Note: –We can use the instanceof operator to check the runtime type of the object before performing the cast, which allows you to prevent ClassCastException

We have basically done DownCasting here , thats the reason it looks messy and even not recommended in java to go for DownCasting . Lets Say if you have a cenario where you really want to go ahead with DownCasting in Java . You have to keep following point in mind

  1. Never Buy for Downcasting .
  2. Always Prefer doing instance of check in case of DownCasting .
  3. UpCasting in java is a Much Safer option always .

2. ByPass the Runtime Error:-

So the Updated and Corrected Code would look like,

Animal animal = new Man();
Man man= (Man) animal;

Explanation:-

So what happens here,

Basically, we are telling the compiler that we know runtime type of the object .Hence we don’t get a compilation error here.

There will not be any runtime error as , the cast is possible because at runtime animalis actually a Man even though the static type of animal is Animal.

Keep in mind , if there is an Inheritance with IS – A relationship we can do Upcasting in Java in every scenario.