wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt in casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt in casting " Watch "doubt in casting " New topic
Author

doubt in casting

Jyothsna Panchagnula
Ranch Hand

Joined: Jul 11, 2005
Posts: 113
Source:-http://www.go4java.20m.com/mock1.htm
.Which one of the following statements will give you an Exception when
placed at line no.10?Select one correct answer.

I see that all the choices are correct.


thanks,
Jyothsna
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032




Hi Jyotshna,

2. is implicit upcasting.
3. is explicit downcasting with no error.
4. is implicit upcasting.

The problem is 1. You are doing explicit downcasting, so it will compile fine, but at runtime the JVM will realize that reference a actually doesn't point to a BB object, so a ClassCastException will be thrown.

The difference between 1. and 3. is that in 1., the a reference points to an AA object, which is not a BB object, so it is a runtime error. However, 3. works fine because reference c, which is of type AA, actually points to a BB object.
[ December 23, 2008: Message edited by: Ruben Soto ]

All code in my posts, unless a source is explicitly mentioned, is my own.
Jyothsna Panchagnula
Ranch Hand

Joined: Jul 11, 2005
Posts: 113
Hello Robert,

Thanks for the clarification!!!
Its helpful indeed.


-Jyothsna
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Glad that my explanation was helpful!
Himalay Majumdar
Ranch Hand

Joined: Sep 28, 2008
Posts: 324
Whenever there is explicit downcasting one needs to makes sure if the Superclass ref_variable referencing to the Subclass object.

FYI.. Dog d = new Animal() is illegal..i.e in the above code one cant say as dog cannot behave like any other Animal.

so BB x = new AA(); is not legal for obvious reasons.
[ December 23, 2008: Message edited by: Himalay Majumdar ]

SCJP 1.6, SCWCD 5.0, SCBCD 5.0 [loading..]
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 813

Is-A test is the best to sort out this kind of problems.

Lion Is-A Animal
but Animal Is-A Lion?? No..so you cannot cast Animal to a Loin


Regards, Sud.
SCJP 5 ScjpFAQ JLS
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Whenever there is explicit downcasting one needs to makes sure if the Superclass ref_variable referencing to the Subclass object.

But it's not necessary that the Superclass ref_variable references a Subclass object, it could also refer to an object of any of the Subclass' subclasses. The object it references simply needs to pass the Is-A test for the subclass type.
Himalay Majumdar
Ranch Hand

Joined: Sep 28, 2008
Posts: 324
Yes, thats right.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt in casting
 
Similar Threads
Casting Doubt
Confused for threading ?
mock exam query
Object Casting
Another examLab casting question