aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Can an Anonymous Inner Class implement more than one interface in Java 6 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 "Can an Anonymous Inner Class implement more than one interface in Java 6" Watch "Can an Anonymous Inner Class implement more than one interface in Java 6" New topic
Author

Can an Anonymous Inner Class implement more than one interface in Java 6

Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206
An anonymous inner class can extend one subclass or implement one
interface. Unlike non-anonymous classes (inner or otherwise), an anonymous
inner class cannot do both. In other words, it cannot both extend a class and
implement an interface, nor can it implement more than one interface.
Chapter 8 p.658 K&B SCJP for Java 5


The following code compiles and runs. Can I expect this behavior with both Java 5 and Java 6? I have a Java 6 on my computer.




C:\TEMP>java Feed
eat
graze
munch
eat2
munch2


Thanks, Harry
~
~
~
~
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
You create an anonymous class of type Ranch, which is of type Range and Grassland.
This is perfectly legal, since Range and Grassland belongs to Ranch.

You could not for instance implement Range and Grassland on it's own.


JDBCSupport - An easy to use, light-weight JDBC framework -
Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206
Thanks for your reply, Sebastian. But the code below compiles and runs just fine. I instantiate an implementation of interface Range and an implementation of interface Grassland. The output is below in red.

-Harry







C:\TEMP>java Feed
graze3
munch3
eat
graze
munch
eat2
munch2
Meredith Johnson
Greenhorn

Joined: Jun 19, 2007
Posts: 3
"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.
"
K&B2008, pg 678.

What I think is as follows.

In your case, first of all, you have Ranch (Interface) separately extend Range (Interface) and Grassland (Interface). In Feed class, there is a definition of an implementer of Range interface which is anonymous. So, that is fine as the anonymous one DIRECTLY implements only one interface (Range). The same goes to the second anonymous implementer (of Grassland interface). Thus, everything is fine until line 28 of your second version of code.

Then you create an instance of a new implementer of Ranch which already has the obligations of three interfaces (Range, Grassland and Ranch itself). So, in this case, even though anonymous Ranch implementer has to implement all three abstract methods in all three interfaces, what it is currently doing is just DIRECTLY implementing ONLY ONE interface (Ranch). Again, everything is fine until line 36 and the idea of one direct implementer still holds true.

Next comes the anonymous subclass of Feed (Feed = implementer of Ranch). In this case, eat() and munch() methods overrides the version of methods in the Feed class itself and the anonymous class also has a method of its own (homo()). This anonymous class too is DIRECTLY extending ONLY ONE class without DIRECTLY implementing another interface apart from OVERRIDING some methods (from the interfaces) of the Feed class.

Therefore, in all cases, Ranch anonymous implementer and Feed anonymous subclass DIRECTLY implements ONLY ONE interface or DIRECTLY extends ONLY ONE superclass respectively. The others are all because of the interface the anonymous one implement or the superclass the anonymous one extends.

Harry: Sorry for my long text and sorry if the explanation make you confused.
Senior ranchers: If I am wrong in something, please correct.

Thank you.
Anastasia Sirotenko
Ranch Hand

Joined: Jul 20, 2009
Posts: 64
Harry Henriques wrote:Thanks for your reply, Sebastian. But the code below compiles and runs just fine. I instantiate an implementation of interface Range and an implementation of interface Grassland. The output is below in red.


You not clearly understand what means "can not extend more then one class or implement more then one interface"
The extended class can implement lots of interfaces itself, or implemented interface can extend from lots of other interfaces. Just like it shows in your code.
You certainly can do this:


But you cannot do something like this:


[SCJP 6.0]
Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206
The extended class can implement lots of interfaces itself, or implemented interface can extend from lots of other interfaces. Just like it shows in your code.


Thanks for the explanations. I just wanted to make sure that this isn't a Java 6 feature ONLY. I know that this works on Java 6, but I'm preparing to take the SCJP 5 exam. My PC is running Java 6, so I can't test on Java 5.

Meredith, I'm not sure that I follow your code walk-thru. There aren't any problems with the code running on Java 6. The code examples compile and run without "compilation failures", "compilation warnings", or "exceptions." I'm unsure about Java 5, though.

Thanks.
Anastasia Sirotenko
Ranch Hand

Joined: Jul 20, 2009
Posts: 64
Harry Henriques wrote:I just wanted to make sure that this isn't a Java 6 feature ONLY.
It is same for java 5.
by the way, you can get any older jdk and manage your IDE or OS to run them
Meredith Johnson
Greenhorn

Joined: Jun 19, 2007
Posts: 3
Yes, there was no problem in the code. The walkthrough just emphasized the "DIRECTLY IMPLEMENTING OR SUBCLASSING THING" by an anonymous inner class.
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

I guess the new keyword can only be used to check on with one class and then create the Object. So there was no question of using 2 implementers.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 784

As per my knowledge anonymous inner class implement only one interface.


SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206
Ninad Kulkarni wrote:As per my knowledge anonymous inner class implement only one interface.


As per my knowledge, an anonymous inner class can extend only one class (an anonymous inner class is a subclass of the class that is visibly instantiated with the "new" operator), or an anonymous inner class can implement only one interface. I think the following is also true. An anonymous inner class can extend one class, and that super class may implement many interfaces. An anonymous inner class can implement only one interface, and that one interface can extend many other interfaces. This is true in Java 6, and I assume that it is true in Java 5.

-Harry
Meredith Johnson
Greenhorn

Joined: Jun 19, 2007
Posts: 3
Even if you have JDK 6, you can do something as follows-
>javac -source 1.5 Feed.java
Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206
Thank you Meredith. I forgot that I could do this.

> javac -source 1.5 Feed.java

Thanks,

Harry
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Can an Anonymous Inner Class implement more than one interface in Java 6
 
Similar Threads
Implementing interfaces
Anonymous class implementing Interface
anonymous inner classs extends Object or not ?
An anonymous inner class.