• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Casting doubt

 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Pranav Bhatt
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Peter Ricke
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Erratum:

i wrote


Object o = new Juicy_Fruit();

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



but wanted to write :

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does this code mean? Does it mean to cast twice? I am not getting it.
Please explain.
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Thanks that was the real help now i guess am clear with the concept of casting. thanks again
 
Pranav Bhatt
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. I wasn't aware that you can cast more than once.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic