Question about casting

  • Thread starter iamrichardjones
  • Start date
I

iamrichardjones

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
 
L

Lothar Kimmeringer

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: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

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

iamrichardjones

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: (e-mail address removed)
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top