Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Casting problem 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 "Casting problem" Watch "Casting problem" New topic
Author

Casting problem

Tony King
Greenhorn

Joined: Oct 26, 2009
Posts: 9
Hi, I have a new doubt about casting. I don' t understand if it's illegal to cast the father class to child class, a class to an interface and an interface to a class (suppose class implements interface). I don't understand the general rule so don't understand why following code compiles ad runs successfully:




Please help me to understand the reasoning behind thi code from line 16 to line 19.
tks

bye
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
Any Interface can be cast to any non final Class type eventhough the class did not implement the interface also if that interface points to any object that could not be castable then you will get runtime exception.

E.g. Line 16.Class A did not implement the interface Min but still casting is allowed.


SO in this case m is the reference of type interface which is not pointing to any object so that it can be cast to any class type.

Also you know Superclass reference can point to any subclass object but not vice versa and you need a cast to sub type if you are down casting it.

In your case A can point to B without any cast because A is a superclass of B.But if you want to do it other way then you have to cast like

B b=(B)a; //assuming a is of type A

If the reference "a' points to any object that is not B or sub type of B then you will get runtime exception.



SCJP 6,SCWCD 5,SCBCD 5

Failure is not an option.
zhong chen
Greenhorn

Joined: Oct 12, 2009
Posts: 24
My approach to see if the code compiles is:

1. determine the class relationships:
B is an A,
C is a B and is an A
B is a Min
C is a Min
So:


2. look at the reference declarations only


3. check each assignment:


For runtime, you need to check the actual object the references refers to (what's behind the new operator?).
If the object type inherits or implements the assigned type, it succeed without explicit cast;
If the object type is a super type of the assiged type, you need an explict cast, otherwise exception raises.
Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

Siva Masilamani wrote:Any Interface can be cast to any non final Class type eventhough the class did not implement the interface

WHY? This blows my mind off. Ofcourse, you are right. An interface can be cast to another class which does NOT implement it. But that makes no sense. That interface object can NEVER be an object of that class, so why does Java compiler allow it, and why no runtime ClassCastException?

Please take a look at the following code:


This prints the following:
m1 instanceof Diamond: FALSE

This makes no sense whatsoever.
Firstly, why allow this type of casting (line 1) ???
Secondly, how can a variable d1, declared as a type of class Diamond and cast to such, is NOT an instanceof Diamond???

Any enlightenment?


"A problem well stated is a problem half solved.” - Charles F. Kettering
SCJP 6, OCPJWCD
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39

Nidhi Sar wrote:
Any enlightenment?


Try...



Notice that the object IS-A Diamond, and IS-A Moldable.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

That interface object can NEVER be an object of that class

Nope, the cast/instanceof comparison might be successful at run time

You see, the cast/instanceof comparison is allowed by the compiler because there might be a class somewhere that extends that class and that interface. This is why it is allowed. But if the class C is final, then we know that there can't be a sub-class of class C, so the cast/instanceof comparison will NEVER succeed in that case and the compiler will give an error...

[Henry posted before me ]


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

Thanks guys, great explanation.

@ PlasticDiamond !!
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

infact , you are not creating any object here[interface is a specification].

Tony King
Greenhorn

Joined: Oct 26, 2009
Posts: 9
Ok, thank you for your replies.

Now I should have understood why my code is good for compiler but i don't understand why there are not runtime error at line 18 (Min m2 = (C) b2;).

What is the type of b2 object (not reference) at line 18?

If I change this line with:

it gives me runtime error.. why? Is "general rule" that I cannot cast a father object (like B) to child object (like c) or vice versa?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39

Tony King wrote:
Now I should have understood why my code is good for compiler but i don't understand why there are not runtime error at line 18 (Min m2 = (C) b2;).

What is the type of b2 object (not reference) at line 18?


If you follow your program, you will see that b2 had the value of null, prior to line 18. A null can be casted to any class type.

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39

Tony King wrote:
If I change this line with:

it gives me runtime error.. why?


At runtime, you can't cast an object to some type that it is not. In this case, a B instance is not a C type.

Henry
zhong chen
Greenhorn

Joined: Oct 12, 2009
Posts: 24
Tony,

Henry is right. You can always follow this practice to determine whether a ClassCastException will throw or not. To be more specific, what object on the heap does b2 point to? You can do a reverse tracing:
before line 18:
b2 --> a2
a2 --> m
m --> b
b --> m
m = null

You see what b2 currently points to is null. You can assign null to any non-primitive type. The point here is you need to trace back to the most resent new operator or null assignment. If the type behind new operator is not something that can be assigned to Min m2, ClassCastException will throw.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Casting problem
 
Similar Threads
examlab - 1 [reference variable casting]
ExamLab Question
Reference variable casting w.r.t interfaces
Casting problem
Downcasting and upcasting Objects