• 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
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
Bartenders:
  • Himai Minh
  • Carey Brown
  • Frits Walraven

Object Reference Casting

 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Could anyone, please, explain why the following code does not compile:
<code>
interface Interface {
}
final class Test implements Serializable {
public static void main(String[] args) {
Interface inter;
Test t = (Test)inter;
}
}
</code>
With the error:
Cannot cast e to Test.
I thought I was following the rule:
If the Oldtype is an interface and the Newtype is a final class then the Newtype must implement interface or Serializable.
Thanks.
Yoo-Jin.
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,
I don't know why you would expect it to compile at all ...
You are declaring a reference named inter but giving it no value, then you are trying to use it (even trying to cast it no less!).
Once you give inter a value you can't just perform one cast since your class doesn't implement the interface itself. Look at the following code.

Regards,
Manfred
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code example above compiles - but when I try to run it I get a class cast exception: InterfaceUser.
Unfortunately - I'm not sure why. Also, I thought that in order to cast an interface to a final class, the final class had to implement that interface. Help?
 
Yoo-Jin Lee
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys for your responses.
I'm still unclear as to why you can cast Interface to Serializable, or since it does not extend it. Is it like Object where all classes inherit from it automatically?
I read this rule from "Complete Java 2 Certification Guide", pg 121: If the Oldtype is an interface and the Newtype is a final class then the Newtype must implement interface or Serializable.
If someone could perhaps enlighten me on it I would really appreciate it.
cheers,
Yoo-Jin.
[This message has been edited by Yoo-Jin Lee (edited March 15, 2001).]
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Yoo-Jin,
JLS �5.5 Casting Conversion states you won't get a compile-error when you cast a non-final class type to an interface since a sub-class might implement the interface.
By inference, this is not true for a final class as a final class cannot have any subclasses therefore, if the final class does not implement the interface, you'll get a compile error.

Hope that helps
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Jane Griscti (edited March 15, 2001).]
[This message has been edited by Jane Griscti (edited March 15, 2001).]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic