1. What is Upcasting and downcasting in JAVA ?
2. Why is that so, that upcasting is automatical, but downcasting must be manual?
3. How can we avoid UPCASTING in JAVA?
Asked in/Preparing for : tcs
Reputation : 206


1 Respond



1. What is Upcasting & Downcasting in JAVA ?

Upcasting is done automatically, while downcasting must be manually done by the programmer.


UPCASTING :

Java permits an object of a subclass type to be treated as an object of any superclass type. This is called upcasting. UPCASTING always works if there is a IS-A relationship. the upcasting is always done from lower level class (subclass) to higher level class(superclass) .It is always implicit and never throws exception .

class Animal () {

void show (){s.o.p ("I am mammal");}
void stroke (Animal a){system.out.println ("you hit me with" + a);}

}

class Mammal extends Animal {

void show (){system.out.println ("I am mammal")}

}

class Cat extends Mammal {

void show {system.out.println ("I am printing from Cat class")};

}


public class Test {

public static void main(String[] args) {

Cat c = new Cat();
Mammal m = c; // This is upcasting
c. show();
m.show()
stroke(c); // automatic upcast to an Animal

}

}
output

I am printing from Cat class
I am printing from Cat class
You hit me with Cat@a90653

in "Cat@a90653" Cat is the class name of the object (here c) and memory location of c

As you can see, Cat is still exactly the same Cat after upcasting, it didn't change to a Mammal, it's just being labeled Mammal right now. This is allowed, because Cat is a Mammal.


Although there's no need to for programmer to upcast manually, it's allowed to do.
Consider the following example:

Mammal m = (Mammal)new Cat();

is equal to / is same as

Mammal m = new Cat();

DOWNCASTING:

Downcast can not be done automatically, It has to be implemented by programmer. Downcasting must always be done manually.

Example:

Cat c1 = new Cat();
Animal a = c1; //automatic upcasting to Animal
Cat c2 = (Cat) a; //manual downcasting back to a Cat

Another example of downcast

Mammal m = new cat();
Cat castC = (Cat) m;
m.show();
castC .show();


output is

I am printing from Cat class
I am printing from Cat class

In this case, the cast is possible because at runtime m is actually a Cat even though the static type of m is Mammal.

 

2. Why is that so, that upcasting is automatical, but downcasting must be manual?

Well, you see, upcasting can never fail. But if you have a group of different Animals and want to downcast them all to a Cat, then there's a chance, that some of these Animals are actually Dogs, and process fails, by throwing ClassCastException.

This is where is should introduce an useful feature called "instanceof", which tests if an object is instance of some Class.

 

3. how can we avoid UPCASTING in JAVA?

Using Super keyword we can avoid Upcasting

 

YOU CAN ALSO FIND MANY FREQUENTLY ASKED JAVA INTERVIEW QUESTIONS BY CLICKING HERE Laughing

Admin Team

many thanks for quick response



Written
Exam Questions
BLOG Posts
For study materials
Job Walk-In
Updates across India
Interview round
Questions
Interview or procedure
Experiences
Files
For study materials
Please LIKE our page to Get regular JOB WALK-IN UPDATES across India and STUDY MATERIALS on facebook news feed.