my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Interface trouble Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Interface trouble" Watch "Interface trouble" New topic
Author

Interface trouble

Rajesh Chandra
Ranch Hand

Joined: Jun 13, 2005
Posts: 55
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 29237
    
139

Rajesh,
That is actually an anonymous class. It creates a class for you that implements the given interface.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Seb Mathe
Ranch Hand

Joined: Sep 28, 2005
Posts: 225
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.


Regards,<br />Seb<br /> <br />SCJP 1.4
Rajesh Chandra
Ranch Hand

Joined: Jun 13, 2005
Posts: 55
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

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
p.s. If you are interested, here is a thread in this forum that discusses the problems with calling r.thread().

Layne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interface trouble
 
Similar Threads
Double Click for JList
want to forward MouseEvents from child to parent component
dnd
custom repaint on JButton on mouserollover
interface implementation and inner classes