This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets 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.
 
 
subject: casting with interface
 
Similar Threads
Class within Interface
inheritance
implementing multiple interfaces
Urgent!!!!!!!! private/protected interface
interface within class or interface