Java: an anonymous class as a subclass from an existing implementation interface?

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

Now I want to create an anonymous class that extends from Aand implements IB.

How will it look like? I thought of 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 impossible to create something like an anonymous class?

+3
source share
1 answer

I suppose you could create an abstract, named inner class, which combines the two and extends this with your anonymous class.

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

The bit is awkward, but I don't think you can implement both.

+4
source

Source: https://habr.com/ru/post/1770541/


All Articles