

We will modify the code above, to ensure the correct method is called to fill our shapes as they are drawn. Within the foreach statement, we have to test the type of shape being drawn before we down-cast, we will use the is keyword to test type. The process of converting super classs refernce that pointing to sub-class Object, to sub-class reference is called downcasting. How do we know what type we have to down-cast to in order to call the correct routine to fill the shape? We have Circle and Square in our array of type Shape.

Taking some code from our polymorphism example, as follows: class Program public class ShapeĬonsole.WriteLine("Drawing a SHAPE at ", m_xpos, m_ypos) Our shape class will implement a constructor that will accept an X and Y co-ordinate and a method that will draw our shape. So how do we implement this concept in C#?įirst we need to design our base class, this will be called Shape. So, what we are saying is a shape can take on many forms or has multiple forms. In this article, the concept of typecasting for objects is discussed. A square, rectangle and a triangle are also shapes. Upcasting Vs Downcasting in Java sofiaa Read Discuss Courses Practice Typecasting is one of the most important concepts which basically deals with the conversion of one data type to another datatype implicitly or explicitly.
sfdcfox at 19:34 You shouldn't really use SetAsk yourself, what is a circle? Most would say a circle is a shape. I don't think you'll find documentation on it. According to many searches on the Internet to find a definitive meaning, I have found two that seem to explain it quite nicely, these are "Having many forms" and "Having multiple forms".Ĭonsider the following. Polymorphism is a powerful aspect of object oriented programming. En este artculo, se analiza el concepto de encasillamiento de objetos. This has expanded the article further and when you have finished reading it, you should hopefully understand the three concepts better. Upcasting Vs Downcasting en Java juRudeus Greyrat Typecasting es uno de los conceptos ms importantes que bsicamente se ocupa de la conversin de un tipo de datos a otro tipo de datos implcita o explcitamente. Having thought further about it, one way I found was to introduce the concept of polymorphism to help explain up and down-casting. Having started writing the article, I was struggling to find an ideal way to describe these two concepts. The original aim of this article was to explain Up-casting and Down-casting.
