Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Instance of an Interface

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
consider an anonymous class
xyz.addActionListener(new ActionListener(){public actionPerformed(ActionEvent a){}});
The method addActionListener takes a parameter of ActionListener type but instead of that parameter we can create an anonymous class i.e. an instance of ActionListener interface
what irritates me here is 'new ActionListener()'
'new' means we want to make an instance of an ActionListener object but interfaces can't be instatiated...
'()' means we are calling its constructor but there are no constructors with interfaces......
please someone explain that.
Thanks.

 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With anonymouse classes, you don't give it a class name and anything after new is what the class implements (if it is an interface) or extends (if it is a class). So in your example, new ActionListener() is really saying new Class Anonymous implenets ActionListener().
You are calling the default constructor the new anonymous class, and anonymous classes cannot contain explicit constructors (how can they when they have no name.)
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! remember these points for anonymous classes:
i) As they have got no names so they can not have constructors.
ii) Where ever they are declared, then and there they are instantiated. so in this case ActionListner interface is extended by an anony class and then it is created using new operator. see how it is done on anony classes. remember declare and extned and create at one place.
 
Nasir Khan
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bill and Asim
Now I got it.
'new ActionListener()' is equivalent to
An anonymous class that implements ActionListener interface.
Also
A default constructor is provided(implicitly) to such an anonymous class.
reply
    Bookmark Topic Watch Topic
  • New Topic