Upcast vs downcast1/30/2024 ![]() 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. ![]() 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. A square, rectangle and a triangle are also shapes. Ask yourself, what is a circle? Most would say a circle is a shape. 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. This has expanded the article further and when you have finished reading it, you should hopefully understand the three concepts better. 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. If you still have any questions, feel free to ask for clarifications.The original aim of this article was to explain Up-casting and Down-casting. I hope this clarifies the differences between upcasting and downcasting. This is a classic exmaple of downcasting since the compiler cannot know what the actual type of the object that is returned by the get() method. Since List.get() is declared to return an Object, we must cast it to a String if there is actually a String at index 0 and we want to manipulate it as such. Downcasting is used when we need to develop a code that accesses behaviors of the child class. As an example, we will use the List l declared above: We use it when we need to develop a code that deals with only the parent class. (Generics has changed this, but that's another issue and beyond the scope of this discussion.) In such a situation, you often have to explicitly downcast an object when you get it out of the Collection. ![]() Prior to Java 1.5, all Collections could only store objects as Object references. The compiler will perform an implicit downcast here since ArrayList implements the List interface. But we are assigning it to a reference to List instead. With the simple example above of polymorphism, you should be able to quickly understand what up-casting is, in fact we have already used up-casting in our example. The result is a reference to this ArrayList object. Notice that on the right hand side, we create an ArrayList object with the keyword new. You will probably see this quite often when using Collections. Perhaps I can illustrate with some examples from the Collections Framework: However, downcasting is not completely safe since the compiler cannot know what the actual type of the object is. In other words, upcasting is the process of converting an object of a derived class to an object of its base class. Upcasting is perfectly safe and often can be done automatically. Upcasting is a concept in C that allows us to treat a derived class as its base class. ![]() Downcasting is the reverse: converting a parent reference to a child. If you view an inheritence hierarchy as a tree with Object at the top and all subclasses of Object as its children and so on, then upcasting is when you have a reference to a child and cast it to a parent class or interface type. a variable, return value, etc.) to a subclass or superclass of its declared type. These terms refer to casting an object reference (i.e.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |