Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2007

Tip: Looking for answers? Try searching our database.

Question about casting

Thread view: 
iamrichardjones@googlemail.com - 07 Oct 2007 13:16 GMT
Hi All,

I ahve a question about casting to object and interfaces. Consider the
following code:

---------------------------------------------------------------------------------------------------------------------
package Test

interface RandomInterface{}

class SubClass1 {}
class SubClass2 {}

public  class Test {
   public static void main(String... args){
       SubClass1 subClass1 = new SubClass1();
       SubClass2 subClass2 = new SubClass2();

       RandomInterface i = (RandomInterface) subClass1; //throws
ClassCastException
       SubClass2 newReference = (SubClass2)subClass1; //wont compile
   }
}
---------------------------------------------------------------------------------------------------------------------

When I cast subClass1 to RandomInterface I get a ClassCastException at
runtime. However if I try and cast subClass1 to SubClass2 then I get a
compile time error. Does java apply different casting rules to class
and interfaces?

Thanks in advance
Richard
Lothar Kimmeringer - 07 Oct 2007 13:34 GMT
> When I cast subClass1 to RandomInterface I get a ClassCastException at
> runtime. However if I try and cast subClass1 to SubClass2 then I get a
> compile time error. Does java apply different casting rules to class
> and interfaces?

If the class isn't declared final a subclass can implement an
interface the super-class hasn't. So during compile-time a
non-final class might be implementing an interface the compiler
isn't aware of so the compiler delegates the decision to the
runtime. Casting a class to another that is not a superclass,
the compiler already knows during compile-time that it doesn't
work and can deny that.

If you declare SubClass1 final, you will get a error during
compile-time as well.

Regards, Lothar
Signature

Lothar Kimmeringer                E-Mail: spamfang@kimmeringer.de
              PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
                questions!

iamrichardjones@googlemail.com - 07 Oct 2007 16:01 GMT
> iamrichardjo...@googlemail.com wrote:
> > When I cast subClass1 to RandomInterface I get a ClassCastException at
[quoted text clipped - 20 lines]
> Always remember: The answer is forty-two, there can only be wrong
>                  questions!

Hi Lothar
Thanks for that explanation. That makes sense now
Richard


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.