![]() That ability to not care when it doesn't matter can wonderfully simplify our code.īut it can certainly confuse humans who think of doctors as way up there. That promise is enforced by the compiler and lets us think about this person differently in different contexts where we might and might not care that they are a doctor. (Though they might ask you to step back and put your mask on). Which makes sense, doctors don't stop being human just because you look at them closely. When a class inherits from another it's promising that it has all the details of the one it inherited from. ![]() I like it when semantics agree with structure. But there is also a structural argument to be made here because your code has Doctor inheriting from Human. So far I've based this argument completely on the semantics of the class names: Human & Doctor. When it gives low level details it's a downcast because we're going to look at it from a lower altitude. When casting takes us away from the low level details we call it an upcast because we're going to look at it from a higher altitude. Even the cow doctors.Īs for casting, that's deciding how to think of whatever we're casting. Go a little higher though and the humans and the cows will start to look the same. So being a Doctor is a low level detail when compared to being a Human. But we can still tell we're looking at a human. (Usually one or two in this situation since people don't like being looked down on).Īt this height it's hard to tell if the human is wearing a lab coat and stethoscope or not. When so high that people look like ants it's hard to count how many fingers they're holding up. The metaphor comes from the way some details fade away as we rise high above them in altitude. We know they spend much of their lives in traffic. That isn't to say we know nothing if we know they're a human. If we only know we're talking about a Human we don't know those things. By lower we mean more specific, more exact, more realized.įor example, we know what a Doctor does for a living. But that can only be understood if you buy into our arbitrary higher/lower metaphor.īy higher we mean more general, more vague, more abstract. We will modify the code above, to ensure the correct method is called to fill our shapes as they are drawn.None of those links indicate that Doctor is higher than Human. 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. The original aim of this article was to explain Up-casting and Down-casting.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |