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
Which is bigger float or double?
What are the different ways to handle exceptions?
What is garbage collection? What is the process that is responsible for doing that in java?
What is an immutable class? How to create an immutable class?
Define Multiprogramming and Multiprocessing in java.
You can create a string object as string str = “abc”; why cant a button object be created as button bt = “abc”;? Explain
What is a singleton in genetics?
why are there separate wait and sleep methods? : Java thread
What is the difference between assignment and initialization?
What is a java applet? What is an interface?
What is the difference between a factory and abstract factory pattern?
How to call one constructor from the other constructor ?
What do you know about the garbage collector in java?
What is array size in java?
Explain the difference between static and dynamic binding in java?