Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Anonymous Inner Class from K&B

 
Paulo Aquino
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got this from page 472 of K&B.

One more thing to keep in mind about anonymous interface implementers� they can implement only one interface. There simply isn�t any mechanism to say that your anonymous inner class is going to implement multiple interfaces. In fact, an anonymous inner class can�t even extend a class and implement an interface at the same time. The inner class has to choose either to be a subclass of a named class�and not directly implement any interfaces at all�or to implement a single interface. By directly, we mean actually using the keyword implements as part of the class
declaration. If the anonymous inner class is a subclass of a class type, it automatically becomes an implementer of any interfaces implemented by the superclass.


What did K&B mean when they say "By directly, we mean actually using the keyword implements as part of the class
declaration." ? And also this, If the anonymous inner class is a subclass of a class type, it automatically becomes an implementer of any interfaces implemented by the superclass.

Can anyone give a code example? Thanks.
 
Joe Sondow
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paulo Aquino:

If the anonymous inner class is a subclass of a class type, it automatically becomes an implementer of any interfaces implemented by the superclass.

Can anyone give a code example? Thanks.


Sure, Paulo. See if this helps at all:
 
vinuharan haran
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anonymous class can implement the methods of the interface not implemented by a superclass if it is an abstract class.It can override the methods implemented by superclass if it is not an abstract class.
What K&B mean by directly is....
class super implements i
Here the keyword implements is specified explicitly.
whereas anonymous class can only specify the class name which it extends or the interface name it implements.
Ex:
Super s=new Super(){.....}; or i obj=new i(){....};where i is an interface.
It cannot include the keywords impements or extends explicitly.

Here is a code...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic