aspose file tools*
The moose likes Beginning Java and the fly likes casting with interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "casting with interface" Watch "casting with interface" New topic
Author

casting with interface

Harshana Dias
Ranch Hand

Joined: Jun 11, 2007
Posts: 327
Hey,

Can we cast a interface to an object?

I mean,



But other way works know,


Thank You.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Would look like this..



JDBCSupport - An easy to use, light-weight JDBC framework -
Harshana Dias
Ranch Hand

Joined: Jun 11, 2007
Posts: 327
So it means right hand side be a object that implements the interface right? not just the interface it self
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Well the only way is to have a concrete class implementing the interface.

An interface is an abstract contract that the implementing class has to fulfill. How would you want to instantiate the interface itself?

The ONLY case when you could instantiate an interface is using an anonymous class, hence automatically building an implementer.
Jarred Olson
Ranch Hand

Joined: Jul 31, 2009
Posts: 37
This may help.



Depending on which object is randomly created your output will be either:

A's doStuff() method.
Special A Code.

or:

B's doStuff() method.
Special B Code.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: casting with interface