jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Reference Variable Casting query. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Reference Variable Casting query." Watch "Reference Variable Casting query." New topic
Author

Reference Variable Casting query.

Rahul Choudhary
Greenhorn

Joined: Jul 25, 2012
Posts: 22
Given: Sour extends Blue extends Pan



Then set of 6 new statements given and asked to choose which ones will compile. They are:



Answer: 5th one won't compile. Rest all will compile and run without exception.

Doubt: As a beginner I have understood that LHS should be usually a super class and RHS should be subclass if downcast not required like shown in original statement Pan p4 = new Sour();
I want to further understand on what basis the options are correct or wrong. For instance (1) is correct since p4's object type is still a sub of Pan so assignement is valid. I did not understand for rest. What is checked at compile time and runtime to consider them valid. As in what is matched with what for compile and runtime? I learnt (albeit unsuccessfully) that object type is compared with something at runtime. What is that? If it is an object type, then given above, variable p4's object type is Sour but what is the object type of p6/b2/b3/s1/s2 that is "compared" with p4's to avoid ClassCastException?

Thanks.

P.S. Question from Kathy Sierra's OCJP Practice Exam for Java 6
Also I have shortened those class names they were originally Pancake, BluberryPancake and SourdoughBlueberryPancake! Talk about Kathy Sierra being creative
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

there has been lot of questions asked on casting. it can be confusing for some time. let us start from the beginning. what is casting ? is it conversion ? do objects actually gets converted into other type ? the answer is it depends. consider the following expression

byte b = 9;

b = (byte) 9+3.5 // here the result is a double which actually gets converted to byte . so here casting is actually converting . b now contains 12

so this conversion happens only in case of primitives. no conversion happens when objects are involved. lets take your example , lets take option 3 in your example Blue b2 = (Blue)p4; what does this mean ?

p4 refers to Sour object and your are casting it to Blue. it is actually upcasting or widening reference conversion(but remember there is no conversion). Blue b2 = (Blue)p4; is simply an instruction to the compiler that "hey compiler, i know that p4 is of type Pan but i know that (and i take responsibility) that it is also of another type viz. Blue. so you please treat the object as Blue." compiler obliges you and does it work. however the cast can fail at runtime which will give you ClassCastException.

while attempting questions based on casting i find following rules very helpful. though they are not official rules but they have been arrived from the understanding of the concept of Casting. lets take General scenario

A B = (C) D;

1. C and D should have some kind of relationship. either C IS-A D or viceversa. OTHERWISE COMPILER ERROR

e.g. String a ="hello";
Dog d =(Dog) a; //compiler error because a and Dog are not in hierarchy. common sense also says that how can a String be a Dog

2. A and C should be of the same type or C should be subtype of A. OTHERWISE COMPILER ERROR.

3. this one is important . the object to which D points/refers should be of the same type as C or it should be subtype of C OTHERWISE RUNTIME ERROR.


e.g class Animal { }
class Dog extends Animal { }
class Labrador extends Dog {}


Dog d = new Dog();

Labrador l =(Labrador)d // here runtime error will occur. why? because the object to which d refers is NOT labrador and also it is NOT subtype of Labrador. infact is is supertype. common sense also says that you cannot cast Dog to something more specific. Labrador IS-A DOG. it has got all Dog properties and behaviour. so labrador can be cast to Dog. but not vice versa. Dog IS NOT a labrador.



now try to understand the concept and then only apply these rules to your specific problem. please repost if you still have doubt.
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 734
Rahul Choudhary wrote:Given: Sour extends Blue extends Pan



Then set of 6 new statements given and asked to choose which ones will compile. They are:



Answer: 5th one won't compile. Rest all will compile and run without exception.


The code looks like this:



Sour is a child of Blue. If you assign a parent to its child, it won't compile.
Dog is a child of Animal. Dog d = new Animal() , won't compile. But Animal a = new Dog() will compile.
d = new Animal() means animal is a dog. But any animal is not a dog. This animal can be a cat, a horse....

Sometimes, the code compiles but throws a class cast exception. When you type cast an object to its child class and this object is not the child type, it will throw this exception.
For example:


Does it make sense?
Rahul Choudhary
Greenhorn

Joined: Jul 25, 2012
Posts: 22
@ Gurpreet Singh and @Himai Minh, guys thanks a ton for detailed explanation Appreciate the effort. As of now it's making sense. I'll code a little and anser few more MCQs and get back if I need help. Thanks again
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

you are welcome Rahul
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reference Variable Casting query.