Java: anonymous class as subclass from existing implementing an interface? – Education Career Blog

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?

,

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.

Leave a Comment