• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
~
~
~
~
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Harry Henriques
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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.
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:
 
Harry Henriques
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, there was no problem in the code. The walkthrough just emphasized the "DIRECTLY IMPLEMENTING OR SUBCLASSING THING" by an anonymous inner class.
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per my knowledge anonymous inner class implement only one interface.
 
Harry Henriques
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even if you have JDK 6, you can do something as follows-
>javac -source 1.5 Feed.java
 
Harry Henriques
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Meredith. I forgot that I could do this.

> javac -source 1.5 Feed.java

Thanks,

Harry
 
Run away! Run away! Here, take this tiny ad with you:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic