Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

instanize interface?

 
bean
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 289
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic