• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interface trouble

 
Rajesh Chandra
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is believed that interface cannot be instantiated.But the code below
shows new MouseListener() .isnt it instantiating the interface MouseListener??


component.addMouseListener( new MouseListener() {
public void mousePressed(MouseEvent evt) { . . . }
public void mouseReleased(MouseEvent evt) { . . . }
public void mouseClicked(MouseEvent evt) { . . . }
public void mouseEntered(MouseEvent evt) { . . . }
public void mouseExited(MouseEvent evt) { . . . }
} );

Thanks in advance

regs
Rajesh
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34974
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajesh,
That is actually an anonymous class. It creates a class for you that implements the given interface.
 
Seb Mathe
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code declares an anonymous class which implements MouseListner., and the instanciate it.

Note that the body of this inner class is declared in your code.
 
Rajesh Chandra
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.I know its anonymous class but the new keyword along with interface name confuses me.
Any way is this legal??
Runnable r = new Runnable();
r.run();
I read its legal. if so why
cheers
Rajesh
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rajesh Chandra:
Thanks for the reply.I know its anonymous class but the new keyword along with interface name confuses me.
Any way is this legal??
Runnable r = new Runnable();
r.run();
I read its legal. if so why
cheers
Rajesh


This is not legal. Did you try to compile it? Since Runnable is an interface, you cannot instantiate it. However, you CAN create instantiate an anonymous inner class that implements Runnable like this:

You should note that this code will compile and run. However, Runnable is designed to be used with the Thread class for multi-threaded programs. The problem is that the above code does not use Thread, so the call to r.run() is still running on the main thread. This is not the main point of your question, though, so I'll just let you look into threads more on your own to understand why this is not a good idea.

Layne
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your new MouseListener() was valid because it provided all the required methods in the following curly braces. All that rather complex syntax defined a class that implements MouseListener. The new Runnable(); examle is not because it does not provide the required methods.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
p.s. If you are interested, here is a thread in this forum that discusses the problems with calling r.thread().

Layne
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic