The graph below describes the hierarchy of MyClass
and MyOtherClass
. With Java is it possible to define a variable that can hold values that are either MyClass
and MyOtherClass
, i.e a variable definition that restricts values to instances that are both sub-classes of JComponent
and implementations of MyInterface
so that functions from JComponent
and MyInterface
are simultaneously available?
I realize the normal way to do this would be to create an intermediate abstract class that extends JComponent
and implements MyInterface
, so I am wondering if this behavior is possible without doing this.
The reason behind the strange class hierarchy is that I initially created MyAbstractClass
to be used as a generic visual type and now would like to add some features to it, i.e. MyInterface
, for use in other places. however I will also need to make some new classes that have the same additional features from MyInterface
but are not instances of MyAbstract class. It would be nice if I didn't have to create a duplicate MyAbstractClass
that extends some other intermediate class that extends Jcomponent
and MyInterface
.
o JComponent
|
|\
| \
| \
| o MyInterface
| |
| |\
| | |
|\ | o MyClass
| | |
| o | MyAbstractClass
| | |
| | |
| | /
| |/
| |
| o MyOtherClass