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

Casting doubt

Pranav Bhatt
Ranch Hand

Joined: Mar 20, 2006
Posts: 284
Hi all,
I am having the following program, but am not sure as to use interface reference should i go with #1 or #2 and is #3 the correct use of casting or not? Am very confused with the casting concept, please some one clarify the below example and if you please can help me get the concept of casting.

interface Sweet {}
class Fruit implements Sweet { }
class Juicy_Fruit extends Fruit {}
class Round_Fruit extends Juicy_Fruit {
public static void main( String[] args) {
Juicy_Fruit JF1 = new Juicy_Fruit();
Sweet s=(Fruit)JF1; // #1
Sweet s=(Round_Fruit)JF1; //#2
Juicy_Fruit JF2=(Juicy_Fruit)(Fruit)JF1; //#3
}
}


Thankyou very much
[ December 07, 2007: Message edited by: pranav bhatt ]
Balamurugan Muthiah
Greenhorn

Joined: Mar 14, 2002
Posts: 17
Here are my comments
#1 - The reference is of Sweet interface type and the object is Juicy_Fruit. Hence you can type cast Juicy_Fruit object to either Fruit or Sweet types. No problem with this statement.

#2 - Same as #1. Typecasting to Round_Fruit will give runtime exception.

#3 - The reference is Juicy_Fruit type and object is Juicy_Fruit. You will get compilation error if you typecast it with Sweet or Fruit types. Typecasting to Round_Fruit will give runtime exception.

For most cases, for the hierarchy you specified using the interface reference is better option. Casting (upcast) of the objects can be done based on the functions you need to use. #1 is more suitable for most cases.


Regards<br />Bala<br />SCJP 1.2, SCWCD 1.4
Pranav Bhatt
Ranch Hand

Joined: Mar 20, 2006
Posts: 284
Hi,
Thanks for helping me out but still am not clear with the concept of casting, i mean how in some cases we can cast and in others we can't.I didn't get how you are determining what we can cast and what not and if we can't then why? Why in some we are getting compile error while at other case we are getting trapped at runtime? It would be a great help if you could elaborate the explanation.

Thanks
nico dotti
Ranch Hand

Joined: Oct 09, 2007
Posts: 124
Can you explain what the exact question is associated with this code? If it's whether they'll compile? What's the most efficient use? It seems they'd all compile since you have explicit casting.
Pranav Bhatt
Ranch Hand

Joined: Mar 20, 2006
Posts: 284
Its that which of the options #1,#2,#3 will not give any class cast exception.I wish to know which explicit cast if fine and which one is not and why?
Thanks
Peter Ricke
Greenhorn

Joined: Dec 07, 2007
Posts: 24
In generall, you are allowed to explicit cast only, if a correct type cast is "possible". If the compiler "thinks", it is not, there will be a runtime exception.

e.g.: you declaired:
Juicy_Fruit JF1 = new Juicy_Fruit();


Because it is obvious, that a Juicy_Fruit object never could be e.g. a String object it is not allowed to cast
String s = (String) JF1;

But if you would have declaired

Object o = new Juicy_Fruit();

than String s = (String) JF1 ; would be no problem for the compiler (not talking about runtime right now) because an Object of type Object could be everything, so why not a String?

So for the compiler everything you codes is just fine
#1 is even an implicit cast (or would be, if you didnt tipe it explicite) as JF1 IS A sweet
#2 is ok for compiler (as JF1 could be a Round_Fruit because Round_Fruit IS A Juicy_Fruit
#3 is ok also because Juicy_Fruit IS A Fruit, so a Fruit-object could be a Juicy_Fruit


At runtime you have to ask yourself : is the object really what i�m trying to make out of it?

#2 is problematic because JF1 could be a Round_Fruit but IT IS NOT. So here a ClassCastException will be thrown.


========<br />class a{<br /> a a(a a){return (a)a;}<br />}
Peter Ricke
Greenhorn

Joined: Dec 07, 2007
Posts: 24
Erratum:

i wrote

Object o = new Juicy_Fruit();

than String s = (String) JF1 ; would be no problem


but wanted to write :

Vidhya Ramaswamy
Ranch Hand

Joined: Oct 10, 2007
Posts: 65
What does this code mean? Does it mean to cast twice? I am not getting it.
Please explain.
prajal Mehta
Ranch Hand

Joined: Dec 03, 2007
Posts: 66
Please go thru these links for better understanding of casting and conversions-


http://radio.javaranch.com/corey/2004/04/05/1081179854000.html

http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#20232
[ December 10, 2007: Message edited by: prajal Mehta ]
Sid Robin
Ranch Hand

Joined: Nov 24, 2007
Posts: 53
The following line was taken from the above link :

"If S is a class type: ... If T is an interface type: ... If S is not a final class (�8.1.1), then the cast is always correct at compile time (because even if S does not implement T, a subclass of S might)"


how is he sure that a subclass of S might implement the Interface T ? Should we assume S to be superclass ? please help

Thanks in advance
Sid Robin
Ranch Hand

Joined: Nov 24, 2007
Posts: 53
Got it guys

The above statement means to say that Hoping a subclass of S might implement interface T there wouldn't be any compiler error but if any of the subclass of S fail to Implement the Interface T there will be Runtime error for sure .Am i right in comprehending ?
Pranav Bhatt
Ranch Hand

Joined: Mar 20, 2006
Posts: 284
Hi all,
Thanks that was the real help now i guess am clear with the concept of casting. thanks again
Pranav Bhatt
Ranch Hand

Joined: Mar 20, 2006
Posts: 284
Vidhya,

What does this code mean? Does it mean to cast twice? I am not getting it.
Please explain.

Juicy_Fruit JF2=(Juicy_Fruit)(Fruit)JF1; //#3


As you can see the reference type on LHS is of Juicy_Fruit so if we cast the RHS till Fruit only (i.e. (Fruit)JF1) we will get error as the expected type is Juicy_Fruit thus it has again been casted to Juicy_Fruit. As Fruit is not a Juicy_Fruit.
Hope am right...
Vidhya Ramaswamy
Ranch Hand

Joined: Oct 10, 2007
Posts: 65
Thanks. I wasn't aware that you can cast more than once.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Casting doubt
 
Similar Threads
Question about packages
A question about cast!
need help with (object reference casting)
Casting
Help me please with Generics..