Results 1 to 10 of 10
- 12-29-2012, 05:09 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 11
- Rep Power
- 0
Need some help in understanding object casting
First of all, i have been pissed the whole day trying to understand object casting so bear with me in this thread!
Okays, so...object casting is the concept of bringing an object of a subclass up the hierarchy. But why? I thought all subclasses inherited any methods or variables of their superclasses....gif)
Also, how does this even work? Does the object magically get copy pasted up to another class or does it get cut pasted?.gif)
Searching all over the internet i found different explanations and i just got frustrated..
and to finalize my post i would like to know the difference between this:
Mammal m = new Cat();
and this:
Cat c = new Cat();
Mammal m = c;
Thanks alot for your time
-
Re: Need some help in understanding object casting
Casting does not change an object but changes a variable so that it is a variable of the cast type. This will allow you call methods specific to new type. And you don't cast a child object to a parent one, but visa versa. Again, casting does not change the object itself, and you are at risk of casting a variable to the wrong type, so only do this with care.
Regarding your code, there's no difference.
- 12-29-2012, 08:15 PM #3
Member
- Join Date
- Oct 2012
- Posts
- 11
- Rep Power
- 0
Re: Need some help in understanding object casting
But i thought upcasting and downcasting both existed. What are the benefits of them?
- 12-29-2012, 10:41 PM #4
Member
- Join Date
- Dec 2012
- Posts
- 12
- Rep Power
- 0
- 12-29-2012, 11:55 PM #5
Re: Need some help in understanding object casting
Looking at it by itself, it's easy to ask what's the point? But upcasting grants code flexibility.
How?
It allows a method to accept objects of many types instead of only objects of the parent class - if it accepts the parent class, it accepts the child classes automatically, which makes sense if you think about it: child classes have at least the fields and methods that the parent class had (remember: with inheritance, you can add new methods and members or change existing ones, but you can't delete existing members or methods), so the code will work with the child class as well - even if the child class decides to do something different with the code it's given.
If you think about it, that's a powerful tool: you can feed a bunch of child classes the same code, and they can each decide to do something different with it. Bear in mind that while this may seem like an obvious idea, many other programming languages do not have this feature.
Downcasting is not recommended, but is sometimes a thing of necessity. If a programmer can get away without downcasting, he simply won't do it, as it reduces code reusability. Remember that code reusability is a major aim of Java!
But it is occasionally necessary, even though it lacks flexibility. This is because while a child class is guaranteed to have at least what the parent class had, the same cannot be said in reverse, increasing the possibility of awkward code.Last edited by Daimoth; 12-30-2012 at 12:31 AM.
- 12-30-2012, 10:24 AM #6
Member
- Join Date
- Oct 2012
- Posts
- 11
- Rep Power
- 0
Re: Need some help in understanding object casting
Thanks alot!
So for example if we have ((Duck)myAnimal.flapWings() (and myAnimal was created like this-> Animal myAnimal = new Duck(); )
Does it temporarily make myanimal into a duck so that it can flapwings (which only a duck can do, since duck is a subclass of Animal) or does the myAnimal become a Duck permanently?
- 12-30-2012, 10:44 AM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Re: Need some help in understanding object casting
No, no object is changed whatsoever; there are two types of casts:
1) upcast (from a derived class D to an ancestor class C);
2) downcast (from a class E to another class C).
The compiler can do upcasts so no additional runtime statements are generated; downcasts are performed by the JVM itself; i.e. it checks whether or not the pointer/reference to E actually can be a pointer/reference to C.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-30-2012, 11:07 AM #8
Re: Need some help in understanding object casting
- 12-30-2012, 11:11 AM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Re: Need some help in understanding object casting
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-30-2012, 02:13 PM #10
Re: Need some help in understanding object casting
Similar Threads
-
Need help understanding casting a child object to a parent reference
By ImWithStupid in forum New To JavaReplies: 3Last Post: 08-25-2011, 08:57 AM -
Casting a String object to a Form object
By kakinyim in forum CLDC and MIDPReplies: 0Last Post: 04-23-2011, 11:41 AM -
Object Casting, please help
By DerekRaimann in forum New To JavaReplies: 5Last Post: 12-04-2010, 01:14 PM -
Object casting
By spiderweb in forum Advanced JavaReplies: 5Last Post: 08-20-2009, 05:43 PM -
Object to Int casting
By nn12 in forum New To JavaReplies: 4Last Post: 12-06-2008, 10:58 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks