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 Building Microservices this week in the Design 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: 330
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: 330
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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: casting with interface
 
It's not a secret anymore!