• 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

Q on casting

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

The above code compiles fine but at runtime throws a ClassCastException.
My query is, how is the compilation successful when the interface 'Test' and class 'Check' are no way related.
Plz enlighten me on this.
reshma
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Reshma,
It will compile, because this kind of casting
tells compiler that u r taking the resposibility
of your code. but when u will run the program,
it will check and throw error.
hope it helps
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe you can cast a "Class" to an "Interface" as the example shows at compile time, but if the reverse happens, where you try to cast an Interface to a class, you'll get a compile error.
 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The concept is there might be a super class of check that implements test. So the compiler makes provision for this, by allowing an unrelated class to be castable to an interface type.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sunita, did you intend to say a subclass of Check instead of saying a superclass?
 
sun par
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nope.. Superclass, bcos if superclass implements an interface, the sub class automatically implements the interface.
 
Reshma Shanbhag
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java guru's on ranch plz help me with this question ..
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Tausif. The compiler assumes that the programmer knows what he/she is doing. At run time, the JVM finds that the cast is not legal and so an exception is thrown.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sun par:
...if superclass implements an interface, the sub class automatically implements the interface.


Your statement is correct--"if superclass implements an interface, the sub class automatically implements the interface." However, the compiler knows all about the superclasses and is able to check any assumptions concerning a superclass. Remember, a class has an "extends" clause that specifies the superclass and an implements clause that specifies which interfaces are implemented. A class, however, does not have an "extended by" clause that specifies subclasses so the compiler does can not make any assumptions concerning subclasses. For example, the compiler can not know which interfaces might be implemented by a subclass.
 
Reshma Shanbhag
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whats the difference
'Test t = (Test) c;'
from the above code and
'String s = (String) new StringBuffer();'
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Reshma Pai:
Whats the difference
'Test t = (Test) c;'
from the above code and
'String s = (String) new StringBuffer();'


The following is a quote from
Section 5.5 of the Java Language Specification.
"Some casts can be proven incorrect at compile time; such casts result in a compile-time error."
A cast from StringBuffer to String would fall into the "can be proven incorrect at compile time" category. The compiler knows that a StringBuffer is not a subclass of String so the cast results in a compile time error.
The compiler accepts the following cast
Test t = (Test) c;
Because Test is an interface and because a subclass of Check might implement Test.
The following is another quote from Section 5.5 of the JLS.
The remaining cases involve conversion between reference types. The detailed rules for compile-time correctness checking of a casting conversion of a value of compile-time reference type S (source) to a compile-time reference type T (target) are as follows:
If S is a class type:

If T is a class type, then S and T must be related classes-that is, S and T must be the same class, or S a subclass of T, or T a subclass of S; otherwise a compile-time error occurs.
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).
  • If S is a final class (�8.1.1), then S must implement T, or a compile-time error occurs.

  • [ March 29, 2003: Message edited by: Dan Chisholm ]
     
    reply
      Bookmark Topic Watch Topic
    • New Topic