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

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you tell me, why does this code compile?
Comparable is not implemented in Cat, so I would expect, that no type casting to Comparable is possible.

public class Unexpected {
public static void main(String... args) {
// why does this compile ???
Comparable c = (Comparable) new Cat();
}
}
class Cat { }
 
Ranch Hand
Posts: 30
Eclipse IDE Spring Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sandor,
when you're trying to cast some object to some type you do not perform something illegal from the compiler's point of view. Actually the compiler does not know anything about right-hand part. But when you'll try to invoke this code you'll get a RuntimeException: ClassCastException because you're trying to do cast to a wrong type.
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you do an explicit cast, the compiler trusts the programmer that he/she has done the cast correctly and will perform a runtime check.

Have a look at the casting section at (bottom half of webpage):

http://java.sun.com/docs/books/tutorial/java/IandI/subclasses.html
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

When you do an explicit cast, the compiler trusts the programmer that he/she has done the cast correctly and will perform a runtime check.




ahhh.... hmmmm.... Well, within reason. If the compiler can confirm that there is no way for the explicit cast to work it will complain.

For example, if you have a reference to an object whose class type does not implement a particular interface. And the class type is declared as final, so there is no way to even subclass from the class, in order to implement that interface, then a explicit cast from that reference to an interface should fail.

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a concrete example. Supposed that you have this...



then...



This is allowed -- even though C does not implement the interface I. The compiler only knows it as the class type B, and it is theortically possible for a new class, say D, to subclass B, and implement I.



This is *not* allowed. The compiler knows it as the class type C, and since class type C is a final class, it is not possible for a new class, say E, to subclass C, and implement I.

Henry
 
Sheriff
Posts: 9707
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
Hi! My first ever post on javaranch was on a similar problem. See if this could help you
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic