aspose file tools*
The moose likes Beginning Java and the fly likes Can anonymous inner class implements mutiple interfaces? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can anonymous inner class implements mutiple interfaces?" Watch "Can anonymous inner class implements mutiple interfaces?" New topic
Author

Can anonymous inner class implements mutiple interfaces?

Fox Hu
Ranch Hand

Joined: Jan 23, 2003
Posts: 49
Can anonymous inner class implements mutiple interfaces?
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
No. The syntax does not allow for it.
And whatever would be the REASON for doing it? If you want to add in the same set of methods as defined in multiple interfaces, go ahead. But the only REASON for implementing multiple interfaces is so that the object of this class can be handled by any of those "types". This is not an issue for an anonymous class since it is not going to be going anywhere but exactly where it is declared. You can't hand around a reference to an anonymous class, because it is - well . . . . anonymous .


"JavaRanch, where the deer and the Certified play" - David O'Meara
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

I'm gonna stick my nose in this one

But the only REASON for implementing multiple interfaces is so that the object of this class can be handled by any of those "types".

The *only* reason? I implement an interface to provide behaviour in my object that I think makes sense. That I can handle it through a reference that is any one of the interface types that I'm implementing is a plus.

Having said that, there ARE cases like you describe were being able to handle it through interface types is the primary benefit.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Cindy Glass:
No. The syntax does not allow for it.
And whatever would be the REASON for doing it? If you want to add in the same set of methods as defined in multiple interfaces, go ahead. But the only REASON for implementing multiple interfaces is so that the object of this class can be handled by any of those "types". This is not an issue for an anonymous class since it is not going to be going anywhere but exactly where it is declared. You can't hand around a reference to an anonymous class, because it is - well . . . . anonymous .

But you can hand around a reference to the object - and might like to cast it to another interface.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Originally posted by Mike Curwen:
I implement an interface to provide behaviour in my object that I think makes sense.

You don't need to implement an interface to DO that, an interface does not PROVIDE any behavior. It just quarantees that the class will have that behavior.
All that implementing an interface does is to enforce the implementation of the methods of the interface and advertise it to the rest of the world. The reason for enforcing it is to allow variables of the type of the interface to be able to hold a reference to it and manipulate it accordingly.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can anonymous inner class implements mutiple interfaces?