+ 1

I don't get why we use CLASS casting

I can use the class itself why use cat in animal and stuff like that.. pleaase. ** i need a clear explanation** with a simple example

12th Nov 2016, 7:15 PM
Ramy Alayane
Ramy Alayane - avatar
6 Answers
+ 1
To understand that you need to have very clear understanding of Inheritance. assuming that, I'm coming straight to your point Let's just say, you have a cat, which is child of pet. And you have a dog, which is also a child of pet. Now, you have something too basic, I.e. for a pet, let's just say food(). But it'd be the owner who provides food(). he can own cat or a dog. now, you'll downcast the cat or dog to pet as they have some method say eat_food() and pass it to owner's method food() for upcasting, let's just say you have list of animals, and you need to know the pets. you'll try to upcast each animal, but only few succeed cause only they are pets. thus, you'll get to know which are pets. or, let's just say to pass a pet, and that particular method needs a cat to be passed. pet would be casted to cat (if the passed pet has all the required property and methods or cat). It's not something you'll see everyday but it is important when you're working in a multi level environment. Because each layer would be communicating with the function calls and objects passing and each layer is developed by different team. hope this has cleared your doubts on it. Please make sure that from next time, when posting a question include a brief description and a very proper question tag to get help quickly.
13th Nov 2016, 8:35 AM
Nagendra Prajwal
Nagendra Prajwal - avatar
0
and we need a question which is properly structured and understandable. If possible, explain a little bit more.
12th Nov 2016, 8:52 PM
Nagendra Prajwal
Nagendra Prajwal - avatar
0
are you asking about Inheritance property of an object in Java?
12th Nov 2016, 9:53 PM
Nagendra Prajwal
Nagendra Prajwal - avatar
0
Oh sorry, i meant upcasting and downcasting objects, for example using Animal a = new Dog(); would would we do that and how does it work really? I need detailed information if possible. thankyou
12th Nov 2016, 10:23 PM
Ramy Alayane
Ramy Alayane - avatar
0
What you're asking is about super and subclasses. In the example you provided, you are creating a new object under the animal class, but are instantiating it with the constructor of a subclass. If you create a Car class, and extend it with specific car types, the Car class provides a template for you to build off of when you create subclasses where the method of obtaining and initializing a particular value for variables differs between subclasses. If you have traditional cars where fuel efficiency is determined by miles per gallon, while hybrids are determined by miles per gallon plus miles per amperes(or whatever unit is used), the constructor is going to have different methods of assigning object variables even though they have the same superclass. How and when you implement this is up to you, but that's how it works
16th Nov 2016, 5:11 AM
Hannah
Hannah - avatar
- 1
Your question is not clear But what I guessed is you want to know why we use classes, We use classes for many reasons ... out code will be more easily to read and understand... That is more like real world so we can write it faster ... understand it faster ... and explain and learn faster... And many many reasons ... But it only makes the work easier... So yo can write what ever you want without using classes ... so they are not nessecarry
12th Nov 2016, 9:51 PM
M4hdyar