• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Casting to interface

 
Michal Adamski
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why compiler allows cast classes for any interface?
 
Vassili Vladimir
Ranch Hand
Posts: 1585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

So that Polymorphism comes into play !!!

Best of luck ...
 
Michal Adamski
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A {};

class Test {

A a = (A)(Appendable)this; // OK (or any interface insted of Appendable)
A a = (A)this // Compilation fails
A a = (A)(String)this // Compilation fails
}

Could you explain in more details why is it like this?
 
Henry Wong
author
Marshal
Pie
Posts: 20886
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michal Adamski:
Why compiler allows cast classes for any interface?


The compiler will allow a cast to an interface, when it can't be sure if the object implements that interface.

Try casting a String object to the Map interface. It will not work -- the compiler will complain. The reason is because the String class is final, it doesn't implement the Map interface, nor can it be subclassed to create a String object that implements that interface.

Henry
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Notice also that in the class Test above (which needs a little help to run), the declaration: A a = (A)(Appendable)this; will compile but fails at runtime with a ClassCastException.

I have just realized that we are not casting to an interface but from an interface to the class A.
[ May 30, 2007: Message edited by: Barry Gaunt ]
 
Kaydell Leavitt
Ranch Hand
Posts: 689
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following link explains why object-reference variables can and should be declared as variables, parameters, and return types.

Programming to an interface.

Kaydell

[ Edited to be more on-topic. ]
[ June 01, 2007: Message edited by: Kaydell Leavitt ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Referring to the topic in question, this example:


gives an idea of what you can do. Notice that the cast to A compiles but thing is not actually referring to an A, so it fails at runtime. In the case of the cast to B, thing1 actually refers to a B so it does not give an exception.
 
Neha Monga
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Barry!

In your above code snippet, interface I is being instantiated. As far as I know, interfaces cannot be instantiated. Am I missing something? Pls correct me if I am wrong.

Thanks!
Neha Monga
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your above code snippet, interface I is being instantiated.

No. Maybe you did not notice the curly brackets. The interface is being implemented.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Satou,

Yes. Thats right. Even just now i also noticed. But i remember the code was like just instantiated.

Thanks for that.
[ May 30, 2007: Message edited by: Raghavan Muthu ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic