Question about casting

Discussion in 'Java' started by iamrichardjones@googlemail.com, Oct 7, 2007.

  1. Guest

    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
     
    , Oct 7, 2007
    #1
    1. Advertising

  2. wrote:

    > 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
    --
    Lothar Kimmeringer E-Mail:
    PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

    Always remember: The answer is forty-two, there can only be wrong
    questions!
     
    Lothar Kimmeringer, Oct 7, 2007
    #2
    1. Advertising

  3. Guest

    On 7 Oct, 05:34, Lothar Kimmeringer <> wrote:
    > wrote:
    > > 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
    > --
    > Lothar Kimmeringer E-Mail:
    > PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)
    >
    > Always remember: The answer is forty-two, there can only be wrong
    > questions!


    Hi Lothar
    Thanks for that explanation. That makes sense now
    Richard
     
    , Oct 7, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Remco
    Replies:
    1
    Views:
    365
    Alvin Bruney [MVP]
    Apr 11, 2004
  2. David Eynon

    casting question

    David Eynon, Jul 1, 2003, in forum: Java
    Replies:
    2
    Views:
    870
    Roedy Green
    Jul 1, 2003
  3. kevin
    Replies:
    11
    Views:
    5,832
    Andrew McDonagh
    Jan 8, 2005
  4. Wally Barnes
    Replies:
    3
    Views:
    540
    Wally Barnes
    Nov 20, 2008
  5. Sosuke

    Up casting and down casting

    Sosuke, Dec 20, 2009, in forum: C++
    Replies:
    2
    Views:
    585
    James Kanze
    Dec 20, 2009
Loading...

Share This Page