• Post Reply Bookmark Topic Watch Topic
  • New Topic

anonymous inner class  RSS feed

 
saravanan ragunathan
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..

 
dileep keely
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.
 
Matthew Brown
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:
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • 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.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • 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.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you can. Except when it's final of course.
 
Dieter Quickfend
Bartender
Posts: 543
4
Java Netbeans IDE Redhat
  • 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
Chrome Java Netbeans IDE
  • 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: 56570
172
  • 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
Chrome Java Netbeans IDE
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!