File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes instanize 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "instanize interface?" Watch "instanize interface?" New topic
Author

instanize interface?

bean
Greenhorn

Joined: May 30, 2002
Posts: 3
I know, interface can not be instanized.
however, in green 3, #7.
"java does not allow the creation of a reference to an interface with the new keyword"
I thought this is true, but the answer is false.
I don't get it....
bean
Herbert Maosa
Ranch Hand

Joined: May 03, 2000
Posts: 289
bean, what you think is not only what I think as well, but it is the fact. Interfaces can NEVER be instantiated, infact by WHATEVER means. If indeed the answer is given as you say, then it is mistake here.
Herbert.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
public class ABC extends A implements Runnable {}
Runnable r =new ABC();
So, we can create a reference to an interface using new word.
fan mo
Ranch Hand

Joined: Mar 14, 2000
Posts: 36
We can try running the following code:

interface IDrawable{
void draw();
}
class Painter{
public static IDrawable createMap(){
return new IDrawable(){
public void draw(){
System.out.println("Drawing a new IDrawable");
}
};
};
}
public class NewInterface{
public static void main(String args[])
{
IDrawable[] Drawables = {
Painter.createMap(),
new Painter().createMap()
};
for(int i=0; i<Drawables.length;i++)>
Drawables[i].draw();

}
}
The above use can be often seen in AWT event handling.
I remember the following use is very familar:
button.addActionListener(new ActionListner(){
.....
.....
}
)

Here the ActionListner() is interface. We can meet other similar
situation in AWT event handling.
I think the point is that we create a instance of Anonymous inner
class which extends the interface
 
Don't get me started about those stupid light bulbs.
 
subject: instanize interface?
 
Similar Threads
Failed to marshal exception problem
Sortable Collection with indexed properties
exception over RMI
How To Call Two Different Classes From the Main Program Using the Same API?
When should Servlet utility classes be Servlets and when ordinary Java classes?