• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can anonymous inner class implements mutiple interfaces?

 
Fox Hu
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anonymous inner class implements mutiple interfaces?
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic