Interface for specific classes only?

Is it possible to create an interface that can only be applied to specific classes and subclasses?

If my interface is only added to different subclasses of JComponent, and I need to refer to both JComponent methods and my interface ... how do I do this? At the top of my head, I can do this by adding methods from JComponent to my interface.

It seems awkward. What is the best way to do this?

+5
source share
3 answers

The obvious solution is to add a method to your interface that returns a component (which may be this).

JComponent getComponent();

:

 public interface MyInterface<C extends JComponent> {
     C getComponent();
     [...]
 }

, .

+7

, , :

interface Foo { void frobulize(); }

class Bar {
    <T extends JComponent & Foo> String doFoo(T obj){
        obj.frobulize();
        return obj.getToolTipText();
    }
}

, , ( , ):

class Quux {
    private final Foo foo;
    private final JComponent component;
    public <T extends JComponent & Foo> Quux(T fc){
        foo = fc;
        component = fc;
    }
}
+2

?

, , , - .

, , , .

-1

All Articles