Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement two interfaces in an anonymous class

Tags:

java

android

I have two interfaces:

interface A {     void foo(); }  interface B {     void bar(); } 

I am able to create anonymous instances of classes implementing either of these interfaces like so:

new A() {     void foo() {} } 

or:

new B() {     void bar() {} } 

I want to create an anonymous class that implements both interfaces. Something like (the fictitious):

new A implements B {     void foo() {}     void bar() {} } 

This obviously gives a compile error: "B cannot be resolved to a type".

The workaround is quite simple:

class Aggregate implements A, B {     void foo() {}     void bar() {} } 

I then use Aggregate where ever I would have used the anonymous class.

I was wondering if it is even legal for an anonymous class to implement two interfaces.

like image 701
Code Poet Avatar asked Nov 17 '11 08:11

Code Poet


People also ask

Can an anonymous inner class implement multiple interfaces?

A normal class can implement any number of interfaces but the anonymous inner class can implement only one interface at a time.

Can a class implements 2 interfaces?

Your class can implement more than one interface, so the implements keyword is followed by a comma-separated list of the interfaces implemented by the class.

Can an anonymous class implement an interface in Java?

The syntax of anonymous classes does not allow us to make them implement multiple interfaces.

Can anonymous class have multiple methods?

You can't. The only way to be able to call multiple methods is to assign the anonymous class instance to some variable.


2 Answers

"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. " (http://scjp.wikidot.com/nested-classes)

like image 151
dbf Avatar answered Sep 25 '22 10:09

dbf


If you are determined to do this, you could declare a third interface, C:

public interface C extends A, B { } 

In this way, you can declare a single anonymous inner class, which is an implementation of C.

A complete example might look like:

public class MyClass {    public interface A {     void foo();   }    public interface B {     void bar();   }    public interface C extends A, B {     void baz();   }    public void doIt(C c) {     c.foo();     c.bar();     c.baz();   }    public static void main(String[] args) {     MyClass mc = new MyClass();      mc.doIt(new C() {       @Override       public void foo() {         System.out.println("foo()");       }        @Override       public void bar() {         System.out.println("bar()");       }        @Override       public void baz() {         System.out.println("baz()");       }     });   }  } 

The output of this example is:

foo() bar() baz() 
like image 38
J Steven Perry Avatar answered Sep 26 '22 10:09

J Steven Perry