Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

interface for only certain classes?

Can you create an interface which can only be applied to certain classes and subclasses?

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

This seems clumsy. What is a better way to do this?

like image 864
jedierikb Avatar asked Aug 24 '09 05:08

jedierikb


1 Answers

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

JComponent getComponent();

Or even genericise your interface:

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

It's not great design, but it should work.

like image 182
Tom Hawtin - tackline Avatar answered Sep 20 '22 10:09

Tom Hawtin - tackline