3

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?

3
  • how doesn't it work? what error are you getting? Commented Oct 20, 2010 at 14:05
  • I think your design is wrong.Class A already offers the functionality of IA and you want it to implement also IB that extends IA? Class A should just implement IB Commented Oct 20, 2010 at 14:23
  • Class A should not (and does not and can not) implement IB (because it must not be abstract). Commented Oct 20, 2010 at 14:32

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.