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
posted
0
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
posted
0
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
posted
0
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