wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes didn't understant class castings between parent and child (all cases) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "didn Watch "didn New topic
Author

didn't understant class castings between parent and child (all cases)

Pratap koritala
Ranch Hand

Joined: Sep 27, 2006
Posts: 252


Whats the logic behind this,and how to remember this?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

  • Upcasting (from a subtype to a supertype) is always safe, so no explicit cast is needed.
  • Downcasting (from a supertype to a subtype) has the potential to be incorrect, so an explicit cast is required.
  • For example, consider...
    In this case, a Cat is always an Animal. So if you have a reference of type Cat, it's always safe to upcast that reference to type Animal. There's no way to get into trouble, so an explicit cast is not needed.

    On the other hand, an Animal is not always a Cat. (It might be a Goat, for example.) So if you have a reference of type Animal, it's not always safe to downcast that that reference to type Cat, so an explicit cast is needed.

    The cast is a way for the programmer to tell the compiler, "Trust me, I know what I'm doing, and you can treat this instance as a Cat." Now, if the programmer turns out to be wrong and the instance is not really a Cat, then the code will compile (because of the explicit cast), but a ClassCastException will be thrown at runtime.

    Note that because Cat extends Animal, it's perfectly reasonable that a variable of type Animal might reference an instance of Cat. So when the programmer uses an explict cast to say, "Trust me...," the compiler says, "Okay, that's reasonable, so I guess you know what you're doing." On the other hand, if the types were unrelated, the compiler would say, "Sorry, I know you're the programmer, but I just can't believe that a variable of type Animal actually references an instance of ToyBoat." So if there is no relationship, then the code will not compile even with an explicit cast.

    It's important to keep in mind that casting only changes what type the reference is treated as. It does not change the instance itself. The true type of an instance is determined by the constructor that's called with "new." For example, if you call "new Animal," then the instance -- the object created -- is an Animal. You cannot turn this into a Cat by downcasting it.
    [ December 18, 2006: Message edited by: marc weber ]

    "We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
    sscce.org
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: didn't understant class castings between parent and child (all cases)
     
    Similar Threads
    why variables inside interface has to final
    Please explain Garbage Collection
    Double K&B Chapter 3- Question 11
    TYPE CAST
    Wrapper, boxing == huh????