Albert
Albert

Reputation: 68360

Java: anonymous class as subclass from existing implementing an interface?

I have interface IA, interface IB extends IA and class A implements IA.

Now I want to create an anonymous class which extends from A and implements IB.

How would that look like? I thought about something like this:

new A() implements IB { /* ... */ }

( Error: Type mismatch: cannot convert from A to IB )

or:

new IB() extends A { /* ... */ }

( Error: Syntax error on token(s), misplaced construct(s) )

Or is it not possible to create something like that as an anonymous class?

Upvotes: 3

Views: 612

Answers (1)

Jim Downing
Jim Downing

Reputation: 1491

I suppose you could create an abstract, named inner class that combined the two and extend that with your anonymous class.

   private static abstract class AB extends A implements IB {};
    ...
   new AB() {};

Bit clumsy, but I don't think you can implement both.

Upvotes: 4

Related Questions