2

Possible Duplicate:
WHY an Anonymous class in Java can't implement multiple interfaces directly? Simply because of syntax or there is another reason?

Hi all I was wondering why is it that Java anonymous classes couldn't implement more than one interface?

Like what problems will we have if the Java designers allowed anonymous classes to implement more than one interface?

As such:

IMammal, I4legged anonymous_creature = new IMammal, I4legged() {
    {
        //..
    }
};
anonymous_creature.FourLeggedStuff();
anonymous_creature.MammalStuff();
Community
  • 1
  • 1
Pacerier
  • 86,231
  • 106
  • 366
  • 634

5 Answers5

5

Like what problems will we have if the Java designers allowed anonymous classes to implement more than one interface?

In Javas type-system there is precisely one static type for each expression. If you had to choose one static type for anonymous_creature you wouldn't be able to make much use of the variable, which is probably why you wrote

IMammal, I4legged anonymous_creature =
^^^^^^^^^^^^^^^^^

which actually changes Javas type-system fundamentally. (Possibly it has been excluded for the same reason as multiple inheritance, namely in order to keep the language simple.)

Besides, there is a trivial workaround, and that is to introduce a auxiliary interface extending both of them:

interface FourLeggedMammal extends IMammal, I4Legged {
}

and then do

... new FourLeggedMammal() { ... }
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • I guess an "Ambigious method call" compile-time error would happen. – bezmax Nov 24 '11 at 09:26
  • 1
    This might be true with the example the OP gave, but why not allow `Mammal c = new Mammal() implements I4Legged { //I4Leged implementations... }`. I've needed this more than once, and had to go the trivial-workaround-way – Xavi López Nov 24 '11 at 09:26
  • @XaviLópez, but what use would that be? `c` couldn't be used as a `I4Legged` anyway, right? – aioobe Nov 24 '11 at 09:30
  • Why not? It could be just a tagging interface. Maybe I'd like to implement a `Restricted` tagging interface, so that a UI component is marked as restricted. I've needed to do something similar to this, don't remember exactly if with interfaces or annotations. – Xavi López Nov 24 '11 at 09:34
  • @aioobe I think he meant `I4Leg` c = new Mammal() implements I4Leg {..` which in this case it could have been used as an `I4Leg` – Pacerier Nov 24 '11 at 11:47
  • But then it couldn't for instance be passed to a method that accepts a `Mammal`, right? – aioobe Nov 25 '11 at 09:01
2

You can via an abstract class:

public abstract class AFourLeggedMammal implements IMammal, I4legged {
}

then in your code, you can do:

AFourLeggedMammal dog = new AFourLeggedMammal() {

}

dog.FourLeggedStuff();
dog.MammalStuff();
Strelok
  • 50,229
  • 9
  • 102
  • 115
  • 3
    ...obviously. But then it wouldn't be an anonymous class anymore, so this is kind of different. – aioobe Nov 24 '11 at 09:25
  • Well you just changed your answer to the same thing only with an interface. The class in the end is also `anonymous` as the name of the class for `dog` is compiler generated. That's all `anonymous` means. – Strelok Nov 24 '11 at 09:27
1

I can't see any technical problem. But anonymous inner classes should be small. Typically implementing a single method. If you want to implement more then a single interface you are probably better of with a top level class.

Of course if you absolutely have to you can create an interface that combines all the interfaces you want to implement and then create an anonymous class for that. Of course the new interface needs a name ...

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
0

http://books.google.it/books?id=G4ridwFSpIoC&pg=PA472&lpg=PA472#v=onepage&q&f=false Paragraph that starts with "One more thing...".

Viruzzo
  • 3,025
  • 13
  • 13
0

An anonymous class can only implement one interface. Why? It's simply a language design choice. Nothing would make it technically impossible.

If you want to implement two or more interfaces you will have to make it a named class or use an intermediate interface or abstract class which extends (interface) or implements (abstract class) two or more other interfaces. Also, you can only refer to it only by one interface name, not two, just like all other objects in Java.

Michiel Borkent
  • 34,228
  • 15
  • 86
  • 149