• 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

interface and class reference variable casting

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
interface A { }
class B { }
class C {
public static void main(String [] args) {
B ob = new B();
A a = (A) ob;
}
}

In above code class 'B' does not implements interface 'A', even than the object of class 'B' is casted to reference of interface 'A' and no compile error is produced, but at runtime it produces ClassCastException.

Q) Why at compile time compiler produces the error of inconveritable types, like it does for class reference variable casting. ?
 
V K Gupta
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Restated:

Q) Why at compile time, compiler does not give the error of inconveritable types, like it does for class reference variable casting. ?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting question. Had to shoot off to the Java Language specification to find the reason and I'm still not entirely sure I understand. See section 5.5

If S is an interface type:
.
.
.
- If T is a type that is not final (�8.1.1), then if there exists a supertype X of T, and a supertype Y of S, such that both X and Y are provably distinct parameterized types, and that the erasures of X and Y are the same, a compile-time error occurs. Otherwise, the cast is always legal at compile time (because even if T does not implement S, a subclass of T might).

 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In more understandable words, ob could be an instance of a subclass of B that does implement A:

The compiler can't tell, so that's why it's allowed.

Basically, once there is an interface involved (either as the declared type or the type you're checking against), there won't be a compiler error.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rob. That makes sense now (as did the JLS explanation once I'd read it a few times )
reply
    Bookmark Topic Watch Topic
  • New Topic