• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interface trouble

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rajesh,
That is actually an anonymous class. It creates a class for you that implements the given interface.
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
The City calls upon her steadfast protectors. Now for a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic