• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

anonymous inner class

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i heard that we cannot create object for interfaces..
but in the following pgm insteadof the classname the object of the interface ActionListener is created ...
i have confusion in this..

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of-course What you have heard about interfaces is correct.you can't create Object of an interface.
So,in the above scenario it is not creating an instance of ActionListener interface instead its creating a new instance of anonymous implementor of ActionListener.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What that's doing is creating a concrete class that implements ActionListener, and then creating an instance of that class. The reason it's called an anonymous inner class is that it doesn't have a name (not that you provide, anyway - the compiler will create one behind the scenes), but it's still a class just like any other.

Try this, and you'll see:
 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
new XXX() in case of anonymous classes means, a new instance of a class that implements XXX, if XXX is an interface or it is a class that extends XXX, if it is a class.
Anonymous class dont need a name,right...so the designers decided to stick to just the name of the implementing interface/extending class.
 
Marshal
Posts: 70225
282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Vinoth Kumar Kannan wrote:new XXX() . . .

It's actually new XXX(){ . . . } The bit in {} is the body of the class; you can even instantiate a concrete class (I think) with a similar construct.
 
Sheriff
Posts: 21997
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you can. Except when it's final of course.
 
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you use



You have to think it equates to this:



You're not creating an instance of an interface, you're creating an instance of an anonymous inner class that happens to implement that interface. But think about this: if you gave that class a name... it wouldn't be an anonymous inner class anymore, would it?
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was exactly what I wanted to say, Dieter.
 
saravanan ragunathan
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Vinoth Kumar Kannan wrote:new XXX() in case of anonymous classes means, a new instance of a class that implements XXX, if XXX is an interface or it is a class that extends XXX, if it is a class.
Anonymous class dont need a name,right...so the designers decided to stick to just the name of the implementing interface/extending class.



thanks for your information

 
Campbell Ritchie
Marshal
Posts: 70225
282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Vinoth Kumar Kannan wrote:. . . Anonymous class dont need a name,right...so the designers decided to stick to just the name of the implementing interface/extending class.

That is rather confusing; when you compile a Java file with annymous inner classes, the inner classes are called Foo$1, Foo$2, etc.
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Vinoth Kumar Kannan wrote:. . . Anonymous class dont need a name,right...so the designers decided to stick to just the name of the implementing interface/extending class.

That is rather confusing; when you compile a Java file with annymous inner classes, the inner classes are called Foo$1, Foo$2, etc.


Thats true that the compiler creates class files for all inner classes with the name 'OuterClass$InnerClass.class' and when the inner class is an anonymous one it names them 1,2..so on.. The compiler creates it , because without it manipulating/managing inner class objects would be difficult.
For an anonymous class, name is not needed as we are never going to instantiate it explicitly.

is not going to work - thats something the compiler creates for itself.
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic