Results 1 to 10 of 10
  1. #1
    Elaforestis is offline Member
    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0

    Default 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...
    Also, how does this even work? Does the object magically get copy pasted up to another class or does it get cut pasted?
    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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  3. #3
    Elaforestis is offline Member
    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0

    Default Re: Need some help in understanding object casting

    But i thought upcasting and downcasting both existed. What are the benefits of them?

  4. #4
    ieee488 is offline Member
    Join Date
    Dec 2012
    Posts
    12
    Rep Power
    0

    Default Re: Need some help in understanding object casting

    Quote Originally Posted by Elaforestis View Post
    But i thought upcasting and downcasting both existed. What are the benefits of them?
    You should buy a book.
    I personally like Java: A Beginner's Guide by Herb Schildt.

  5. #5
    Daimoth's Avatar
    Daimoth is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default 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.

  6. #6
    Elaforestis is offline Member
    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0

    Default 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?

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: Need some help in understanding object casting

    Quote Originally Posted by Elaforestis View Post
    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?
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default Re: Need some help in understanding object casting

    Quote Originally Posted by JosAH View Post
    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.
    For primitive casting an implicit downcast will only be done if the downcast value fits, example assigning a integer to a double without explicit cast will work: double d = 100L. But the statement int x = 39.98 will not compile without explicit cast: int x = (int)39.98.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: Need some help in understanding object casting

    Quote Originally Posted by j2me64 View Post
    For primitive casting an implicit downcast will only be done if the downcast value fits, example assigning a integer to a double without explicit cast will work: double d = 100L. But the statement int x = 39.98 will not compile without explicit cast: int x = (int)39.98.
    Primitive casts deal with 'narrowing' and 'widening' casts; widening casts can be implicit and narrowing casts have to be explicit and are checked by the compiler. No up or downcasting is involved.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default Re: Need some help in understanding object casting

    Quote Originally Posted by JosAH View Post
    Primitive casts deal with 'narrowing' and 'widening' casts; widening casts can be implicit and narrowing casts have to be explicit and are checked by the compiler. No up or downcasting is involved.
    to be precise: right!

Similar Threads

  1. Replies: 3
    Last Post: 08-25-2011, 08:57 AM
  2. Casting a String object to a Form object
    By kakinyim in forum CLDC and MIDP
    Replies: 0
    Last Post: 04-23-2011, 11:41 AM
  3. Object Casting, please help
    By DerekRaimann in forum New To Java
    Replies: 5
    Last Post: 12-04-2010, 01:14 PM
  4. Object casting
    By spiderweb in forum Advanced Java
    Replies: 5
    Last Post: 08-20-2009, 05:43 PM
  5. Object to Int casting
    By nn12 in forum New To Java
    Replies: 4
    Last Post: 12-06-2008, 10:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •