File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Anonymous Inner Class from K&B Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Anonymous Inner Class from K&B" Watch "Anonymous Inner Class from K&B" New topic
Author

Anonymous Inner Class from K&B

Paulo Aquino
Ranch Hand

Joined: Apr 29, 2002
Posts: 200
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.


Be Afraid...Be very Afraid...
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
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:


SCJA 1.0 (98%), SCJP 1.4 (98%)
vinuharan haran
Ranch Hand

Joined: Feb 26, 2005
Posts: 64
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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Anonymous Inner Class from K&B