![]() ![]() 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. All you need to do, is, place a method inside of the Cat class, that converts the fields and returns a new Dog based on that.None of those links indicate that Doctor is higher than Human. ![]() We just need to write it once, and every Animal gets it through inheritance.Ĭonsider the following example: class Animal Īnd you want to make a Dog out of the Cat. What this means for a programmer, is that we don’t need to write for every possible Animal, that it has health. Cat is also an Animal and a Mammal, which logically means – if Mammals possess mammary glands and Animals are living beings, then Cat also has mammary glands and is living being. Object is Cat’s grandgrandparent, which means Cat is also an Object. Now, if you ask – is Cat an Object – It doesn’t extend Object, it extends Mammal?īy inheritance Cat gets all the properties its ancestors have. By silently, i mean, that Java automatically extends every class from Object class, which isn’t extended from something else, so everything is an Object (except primitives). You can see, that Cat and Dog are both Mammals, which extends from Animal, which silently extends from Object. What we have here, is a simplified version of an Animal Hierarchy. Throughout this tutorial i’m going to use Animal hierarchy to explain how class hierarchy works. Upcasting and downcasting are NOT like casting primitives from one to other, and i believe that’s what causes a lot of confusion, when programmer starts to learn casting objects. Upcasting is done automatically, while downcasting must be manually done by the programmer, and i’m going to give my best to explain why is that so. Java permits an object of a subclass type to be treated as an object of any superclass type. Upcasting and downcasting are important part of Java, which allow us to build complicated programs using simple syntax, and gives us great advantages, like Polymorphism or grouping different objects. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |