Anonymous interface implementation?

  • Thread starter Thomas Kellerer
  • Start date
T

Thomas Kellerer

Hi,

I'm wondering whether I can create a subclass "on the fly" that implements a
certain interface. E.g. a JPanel that implements an interface, something along
the lines

I know I can write:

JPanel p = new JPanel()
{
public void interfaceMethod()
{
// do some stuff here...
}
};

but how do I declare this new class to implement a certain interface (the one
that is implemented with the anonymous inheritance)?

something like:

JPanel p = new JPanel()
implements MyInterface
{
public void interfaceMethod()
{
}
};


Regards
Thomas
 
V

Venkatesh

Hi Thomas,

Your question is kind of unclear. If u have some class like JPanel
whose definition already exists (coz u already have the name of class
as JPanel), then in the class definition u would have implemented the
interface.

I guess u can dynamically create objects that implement interface. I've
seen this technique used generally in the case of event listeners.

-Venkatesh
 
T

Thomas Kellerer

Hi Venkatesh,
I guess u can dynamically create objects that implement interface. I've
seen this technique used generally in the case of event listeners.

Yes I know that, but I do not want to create an new anonymous class that
implements a certain interface (e.g. with new Runnable() { ... })

Your question is kind of unclear. If u have some class like JPanel
whose definition already exists (coz u already have the name of class
as JPanel), then in the class definition u would have implemented the
interface.

The problem is that I cannot change JPanel. As I can create a sub-class "on the
fly" (as I have shown), I just want to add an "implements myInterface" there, so
that the sub-class is recognized as the implementing the interface

Thomas
 
O

Owen Jacobson

Hi,

I'm wondering whether I can create a subclass "on the fly" that implements a
certain interface. E.g. a JPanel that implements an interface, something along
the lines

I know I can write:

JPanel p = new JPanel()
{
public void interfaceMethod()
{
// do some stuff here...
}
};

but how do I declare this new class to implement a certain interface (the one
that is implemented with the anonymous inheritance)?

something like:

JPanel p = new JPanel()
implements MyInterface
{
public void interfaceMethod()
{
}
};

Near as I can tell you can't get there from here. JLS 3 section 15.9
describes the grammar for class instance creation expressions, and the
only token between 'new' and '(' is the name of the class being
instantiated. You'll have to declare an inner or nested class foo extends
JPanel implements MyInterface and instantiate that instead.
 
T

Thomas Hawtin

Thomas said:
but how do I declare this new class to implement a certain interface
(the one that is implemented with the anonymous inheritance)?

something like:

JPanel p = new JPanel()
implements MyInterface
{
public void interfaceMethod()
{
}
};

You can't use anonymous inner classes to subtype two types at once
(excluding Object). There are exceptions, but in general there isn't
much point in trying this. Assigning (a reference to) the object to a
variable means that you will lose one of the types. If you really want
to do it, there are two obvious approaches.

Add an intermediate abstract class:

private static abstract class MyInterfacePanel
extends JPanel
implements MyInterface
{
MyInterfacePanel() {
super();
}
MyInterfacePanel(LayoutManager layout) {
super(layout);
}
}
...
JPanel panel = new MyInterfacePanel() {
// stuff
};

Use a local class instead of anonymous class:

class MyInterfacePanel
extends JPanel
implements MyInterface
{
// methods can use local final variables.
}
JPanel panel = new MyInterfacePanel();

Tom Hawtin
 
T

Thomas Kellerer

Owen Jacobson wrote on 01.06.2006 09:23:
Near as I can tell you can't get there from here. JLS 3 section 15.9
describes the grammar for class instance creation expressions, and the
only token between 'new' and '(' is the name of the class being
instantiated. You'll have to declare an inner or nested class foo extends
JPanel implements MyInterface and instantiate that instead.

Thanks for the answer.

I read the section in the JLS, but was not 100% sure. I think it would be nice
if this was possible. I can live with the nested class.


Thanks
Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top