Click here to Skip to main content
15,895,709 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Can someone share a real time example where up-casting is must if we don't, we wouldn't be able to solve the problem.

Or

Can someone teach what is up-casting and down-casting. i have googled it couldn't understand it yet.
Posted
Comments
Sergey Alexandrovich Kryukov 22-Feb-15 23:36pm    
This is two vague question. Do you have any real problems? Why would not you learn the topic yourself?
Sorry, we don't have resources for tutoring; from the other hand, this issue along is hardly productive enough to become and idea for an article. If you have some real problem, please ask a question, we will gladly help.
—SA
mahakaal 22-Feb-15 23:42pm    
Thanks for your quick reply Sir.
i am trying to understand it and after your suggestion. i will try my best to understand it myself.

1 solution

I think this article explains up and down casting quite well Polymorphism, Up-casting and Down-casting[^].
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-Feb-15 23:42pm    
Well, more or less. I voted 4, and the article probably deserves even less; there is more to that, could be explained better, etc.
—SA
Wendelius 22-Feb-15 23:44pm    
I agree, especially concerning down casting, however hopefully the OP gets a kick start from this.
mahakaal 23-Feb-15 0:02am    
Thankx Mika,

i have gone through the article you have suggested.
There is a statement
shapes[2] = new Circle(300, 300);

What does it mean and what if we don't use the reference of shape and uses the reference of Circle

Circle Cicle1= new Circle(300, 300);

Sorry, May be this question will seems silly as i am not so good in OOP.
Wendelius 23-Feb-15 0:17am    
Shape[] shapes = new Shape[3];
shapes[0] = new Shape(100, 100);
shapes[1] = new Square(200, 200);
shapes[2] = new Circle(300, 300);

If the array would be defined as Circle you wouldn't be able to add a Square into it.
mahakaal 23-Feb-15 0:26am    
Thanks again Mika,

But what is the benefit of assigning derived class object to base class reference.

What if we don't do this and do in the below way
Shape[] shapes = new Shape[3];
shapes[0] = new Shape(100, 100);
shapes[1] = new Square(200, 200);
Circle circle1 = new Circle(300, 300)
foreach (Shape shape in shapes)
{
shape.Draw();
}
circle1.Draw();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900