what are upcasting and downcasting?

Answers were Sorted based on User's Feedback



what are upcasting and downcasting?..

Answer / arjun kumar

When we cast a reference along the class hierarchy in a
direction from the sub classes towards the root, it is an
upcast.
When we cast a reference along the class hierarchy in a
direction from the root class towards the children or
subclasses, it is downcast.

Is This Answer Correct ?    19 Yes 3 No

what are upcasting and downcasting?..

Answer / aravind

Please find the difference between DownCasting and Upcasting
with the beloe example

Base Class is Shape

public class Shape
{
protected int m_xpos;
protected int m_ypos;
public Shape()
{
}
public Shape(int x, int y)
{
m_xpos = x;
m_ypos = y;
}
}

Circle class which extends Shape class

public class Circle extends Shape
{
public Circle()
{
}
public Circle(int x, int y)
{
}
}


Main Class:

class CastingExample{
public static void main(String[] args) {
Shape s = new Circle(); // Upcasting
Circle c;
c = (Circle)s; // Downcasting explicitly we have to
do,this is unsafe.
}
}

Is This Answer Correct ?    10 Yes 3 No

what are upcasting and downcasting?..

Answer / vikram aditya

When reference variable of super class refers to the object of sub class that mechanism is known as Up-casting.
Ex:
A a=new B();

When reference variable of sub class refers to the object of super class that mechanism is known as Down-casting.
Ex:
B b=(B) a;

//Where A is super class and B is subclass
 

Is This Answer Correct ?    6 Yes 0 No

what are upcasting and downcasting?..

Answer / malligontla

A super class reference can be converted into a subclass
reference but this subclass reference is not useful to call
the methods of any of the classes. This convention is known
as Downcasting.

We can convert the subclass reference to a superclass
reference without using the cast operation, because compiler
will internally take care of the casting. This is known as
Upcasting.

Is This Answer Correct ?    3 Yes 2 No

what are upcasting and downcasting?..

Answer / amandee[

when we cast reference variable into subclass then
implicitly casting happens means that up casting and vice versa

Is This Answer Correct ?    1 Yes 9 No

Post New Answer

More Core Java Interview Questions

Can a final variable be null?

0 Answers  


I have a sorting issue with a Hashmap. My constraint is that I MUST use the Hashmap and work with existing code. I do a database query and place the results in a Hashmap. When I iterate thru the Hashmap, it loses the original alphabetical sorting done by the database. So, my problem is that I must sort the results coming out of the Hashmap which is then placed into another class.

1 Answers  


write SQL command for table employee where print first name or last name start like "A" and who is working in domain(angular js,java,dotnet)

1 Answers  


When throws keyword is used?

0 Answers  


Difference between prefix and postfix forms of the ++operator?

3 Answers  






List implementations of list interface?

0 Answers  


what is thread in Java ?

0 Answers  


system.out.println(1 + 3);

8 Answers  


Explain about arraylist?

0 Answers  


How to create an instance of a class without using "new" operator? Plz help me out properly.Thank u.

10 Answers   CSC,


What are the types of collections in java?

0 Answers  


Explain restrictions for using anonymous inner classes?

0 Answers  


Categories