what are upcasting and downcasting?

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the difference between object oriented programming language and object based programming language?

552


Which collections are thread safe in java?

475


What does a method signature consist of?

507


What is %d in printf?

524


Is vector synchronized in java?

547






What do you understand by overloading and overriding in java?

544


What does pointer mean?

544


How do you change an int to a string?

543


Differentiate between class and structure.

624


Difference between a class and an object?

571


What are the differences between getting and load method?

540


What is variable in java?

520


What is the function of compareto in java?

585


Is it possible to use Semaphore/ Mutex in an Interrupt Handler?

532


Can memory leak in java?

580