• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Java - Interface Casting

Garet Jax

Diamond Member
Hello all,

I am reading through a Java book. It says that given the following code:

Oldtype ot;
Newtype nt;
nt = (Newtype)ot;

where OldType and Newtype are both interfaces, this is almost always going to pass Compile time rules. It says the case(s) that won't pass are obscure, but does not give any other details. Can anyone shed some light on the case(s) that won't pass compile time rules in these circumstances?

Thanks in advance.
 
I may not be reading into your question the right way, but here goes:

If an object type inherits from another object type, then you can cast it to it's parent object type.
So, say... you have a Panel, which inherits from a Window or something (I'm making the window part up, I forget what Panel inherits from), you could cast that fine.
I.e. Integer and any object inherits from Object, so I'm pretty sure you can cast anything to Object, etc. However, say I made a Box type and a Wheel type, I'm pretty sure I'd hit a compiler error if I tried to cast Box to Wheel, or Wheel to Box. It's been months since I've worked with Java, I hope this is somewhat helpful.

 
Thanks for the answer, but you did misunderstand the question.

First off, Newtype and Oldtype are interfaces and not classes. Second, I am looking for the obscure case(s) where a programmer would get a compile time error when casting between the two.
 
Oscure cases? How about this one (although it's not exactly obscure):

interfaceIFoo
{
publicvoidFoo();
}

interfaceIFoo2
{
publicvoidFoo();
}

publicclassFooTestimplementsIFoo
{
publicstaticvoidmain(String[]args)
{
IFoof=newFooTest();
IFoo2f2;
f2=(IFoo2)f;
}

publicvoidFoo()
{
System.out.println("Foo");
}
}

This is exactly like your example. How can I cast an IFoo to an IFoo2, and how could I cast a Oldtype interface to a Newtype interface? Perhaps you left off pertinent code, such as the fact that the interfaces inherit from each other? Consider this now:

interfaceIFoo
{
publicvoidFoo();
}

interfaceIFoo2extendsIFoo
{
publicvoidFoo2();
}

publicclassFooTestimplementsIFoo2
{
publicstaticvoidmain(String[]args)
{
IFoof=newFooTest();
IFoo2f2;
f2=(IFoo2)f;

f2.Foo();
}

publicvoidFoo()
{
System.out.println("Foo");
}

publicvoidFoo2()
{
System.out.println("Foo2");
}
}

Now the type that implements IFoo2, FooTest, allows the downcast from IFoo to IFoo2 because the type implements both interfaces through interface inheritance.

There are more obscure circumstances in other languages, like C#, that allow explicit interface implementation. Java doesn't allow this though.
 
Descartes,

You first example compiles fine, but causes a runtime exception with a ClassCast problem. I am looking for examples that cause Compile Time errors not run time errors.

I haven't had a chance to check the second example. Will do that tonight.
 
Originally posted by: DescartesThis is exactly like your example. How can I cast an IFoo to an IFoo2, and how could I cast a Oldtype interface to a Newtype interface? Perhaps you left off pertinent code, such as the fact that the interfaces inherit from each other?

Java actually allows most interface to interface casts to happen with little or no compile time checking. I am trying to figure out exactly the checking that does happen.
 
My fault 😱 I actually read where you said that, but I apparently ignored it. 🙂 The only thing I can think of that would generate a compile-time error when casting between interfaces is when you have two interfaces with the same method name, but a different return type. e.g.:

interfaceIFoo
{
publicvoidFoo();
}

interfaceIFoo2
{
publicintFoo();
}

publicclassFooTestimplementsIFoo
{
publicstaticvoidmain(String[]args)
{
IFoof=newFooTest();
f.Foo();

IFoo2f2=(IFoo2)f;
}

publicvoidFoo()
{
System.out.println("Foo");
}
}

That will throw a ClassCastException because the signature is the same with the exception of the return type. I can't think of any other esoteric examples where a compile-time exception would be thrown.
 
Back
Top