• 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 to an Interface

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,
I came across this very nice article about "Casting to an Interface" : http://radio.javaranch.com/corey/replyToComment.action?entry=1081179854000&comment=1131993588581#form .

Wherein I came across this line under JLS

"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).



Now a class implementing T,of which S is a subclass is a clear case where the program would run successfully. But how a subclass of S implementing T would be a reason for the JVM not to crib is a bit beyond my comprehension.

Can anyone help?

 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't understand your question properly, but this is what the statement is trying to say

Here the cast is allowed by the compiler between S and T even though S doesn't implement T. This is because there can be subclasses of S (like Sub in our case) which extend S and implement T...
 
Krutika Ravi
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for explaining..I got it where i was going wrong

I was too fixated on this line
S s= new S(); to think about polymorphism. Silly me!
 
Ranch Hand
Posts: 30
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
tricky one
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ankit : Let's strip it down even more. It looks like any object can be
cast to any interface. Check this out. It compiles fine:Jim ... ...
 
Sandy Ghosh
Ranch Hand
Posts: 30
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Quite obvious, any sub class of Object may implement the interface, that's why it is getting compiled..
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the extreme example of the dangers of down-casting. A down-cast object
is expected to have more or different behavior than the compiled type. So with
the cast to AnyInt, my poor obj can be expected to implement any method. As
you say, this risk is pretty obvious, but it may deserve mention once again.

Jim ... ...
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this example demonstrates that the compiler only considers the type of the reference variable
(which once set cannot be changed);
if only classes are involved these types must be in the same inheritance tree coz any class can only have one direct parent class;
if, as this example shows, interfaces are involved the compiler must allow for any theoretical possibilities among the types
as any class can extend > 1 interface.

only at runtime are the objects pointed to by the reference variable considered and then by the jvm.

these rules hold even if, at runtime time, a ClassCastException must be thrown.
given the example above, the following change will compile even though a ClassCastException is guaranteed.


 
I am going down to the lab. Do NOT let anyone in. Not even this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic