• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Casting Doubt

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

My understanding is that cast works only in case of class hierarchy i.e. if there is some relationship between the two classes.
Following won't compile, since there is no relationship between a HashMap and String.


But why does the following compile



I am not able to figure out.....
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try compiling your code then run it:



You will encounter a ClassCastException at runtime.
 
Amrit Kashyap
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks jart,

I know it will throw ClassCastException at runtime.

My question is that why it's getting compiled.

Map Interface does not extends Collection interface and hence they don't have any relationship as per my understanding.

You see if I cast Map reference to String, it won't even get compiled. Then why a Map reference can be casted to Colletion.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


These types of concepts breaks the rule some time..... I m really confused... Good finding. Friends any one has logical reason for this...
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Roy, how you doing? That's a hard question, but i'll try to explain it...

We know that ArrayList al, can refers to any ArrayList, or any Subclass of ArrayList. We also know that ArrayList dont implements the interface [EMAIL]Map[/EMAIL]. BUT, we dont know if any of its subclasses do. We could have the following situation:



As the compiler can't know at compile time what ArrayList al is really refering to (becouse of the polymorphism), it compiles just fine...

PS.: if you really try to extend ArrayList and implements Map at the same time, you'll get an error, but that's for a different reason, sometimes called (diamond problem).

Hope Helped.

Tks
 
Amrit Kashyap
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Thiago,

Can you provide some more detail by giving some more realsitic example?

As you said we cannot extends ArrayList and implements Map interface. It's right because of following reason:

1. ArrayList implements Collection interface and Collection interface has method remove with following signature:

public boolean remove(Object v)

2. The Map interface also have remove method, but with different signature

public Object remove(Object key)

(I am not using generics syntax here for clarity)

So when you try to extend ArrayList and implement Map interface at the same time, you get compiler error.

If I go by the logic which you have given me, in that case I can cast any NON-FINAL class to any other incompatible type.

But I cannot do that. The compiler stops straightahead by givng error of "incompatible type".

So please give me some more realistic example.
 
Amrit Kashyap
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And this one also get's compiled





Can anybody give me some logical reason for the same..........

 
Amrit Kashyap
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I think I've found the solution to this casting problem.

It happens only in case of Interfaces.

Consider the following case:

 
Thiago Furuchima
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As asked, here goes a better (i think) example:



If the class A were marked final, lines 4 and 5 would not compile (even with cast), because the only way "d" could refers to a A object, is if the class A directly implements D.

Tks.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I am new to this but after reading all of your post i got a question that why peoples are telling that they dont have any relation ship but can the please see the declration of ArrayList and HashMap
may be you find some result
public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable

public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable

I got a relation as both class are implementing Cloneable, java.io.Serializable

I dont know is any one of you will find any result from this or not
Let me know that will it help somthing to remove our problem at all
Thank
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Casting a non-final class to interface will always pass the compiler check. Even if the source class does not implement the target interface, it doesn't necessarily mean its subclass will not. Since such cast has chances to be successful, compiler gives it a pass and relies on runtime check.

However, if you try to cast a final class to an interface it doesn't implement, you will get a compiler error right away. Since final class can't be extended, if it doesn't implement the target interface, compiler can be certain such cast won't have any chance of success. Therefore, it raises a red flag.

You can find more detailed explanation from Java Language Specification.

Hope this helps.
 
On top of spaghetti all covered in cheese, there was this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic