28

Code like:

protected Interface1 varClass1 = new Interface1() {

But I would also like that this anonymous nested class also extends the class Base, something like:

protected Interface1 varClass1 = new Interface1() extends Base {
....

Is this possible in Java?

Maehler
  • 6,111
  • 1
  • 41
  • 46
user810430
  • 11,181
  • 15
  • 38
  • 43
  • 1
    @EranMedan - This question is specifically about doing both at the same time: extending a class while implementing an interface. The other question is about doing either one alone. – David Harkness May 07 '12 at 08:47
  • @DavidHarkness you are right, no more SO at 5am for me – Eran Medan May 07 '12 at 08:49
  • 2
    To me, it feels like your intended anonymous class is a tad schizophrenic. Especially if it needs to interact with fields of the containing class. I strongly suggest you meditate on if what you really need is a slight redesign. – Buhb May 07 '12 at 09:57

2 Answers2

31

An anonymous class can either implement exactly one interface or extend one class.

One workaround is to create a named class that extends Base and implements Interface1, and then use that as the base class for the anonymous class:

public abstract class Base1 extends Base implements Interface1 {}

...

protected Interface1 varClass1 = new Base1() {
   ...
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • 4
    +1, nice solution! Only thing I would add is that if you need to do this, then it is a hint that you are stretching beyond the intended use of anonymous classes and should perhaps consider full named inner classes instead. – mikera May 07 '12 at 08:42
  • 1
    To limit the scope of `Base1` you can even declare it inside the method, right before it is used. – aioobe May 07 '12 at 08:43
  • @aioobe - Inside the *method?* – David Harkness May 07 '12 at 08:48
  • 1
    @DavidHarkness inside the body of the method you can declare a class: `class Hello {}` – Guillaume Polet May 07 '12 at 09:02
  • @aix: Can the inheritance doing with the same when creating the object? – Crazenezz May 07 '12 at 09:02
  • Note that you can declare Base1 within a method (remove the public) and that'd be interesting to declare it abstract. This way it'd look more like what the OP wants. – Jerome May 07 '12 at 09:09
  • @GuillaumePolet - Funny, with anonymous classes I never needed to do this and so never tried. Cool. :) – David Harkness May 07 '12 at 16:30
3

While it is not possible to both extend a class and implement an interface, what you probably what to do can be achieved using delegation.

Just create an instance of Base in the initializer of your anonymous class and use its methods from the methods you implement from Interface1.

EDIT: My answer assumes that you only want to inherit from Base because of its implementation, not because of its interface. This is a valid assumption given that the interface of Baseis not visible after assigning the object to a Interface1 variable. Therefore, I think it's better to use delegation here instead of creating a new class.

rolve
  • 10,083
  • 4
  • 55
  • 75