my dog learned polymorphism
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "instanize interface?" Watch "instanize interface?" New topic

instanize interface?


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....
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.
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 = {
new Painter().createMap()
for(int i=0; i<Drawables.length;i++)>

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
I agree. Here's the link:
subject: instanize interface?