I have two Java class hierarchies that share a common ancestor and implement a common interface. I need to pass a pointer to one of these things to a method in another class.
interface I { ... }
class A extends java.awt.Component implements I { ... }
class B extends java.awt.Component implements I { ... }
class D {
Component c;
I i;
void m(? x) {
c = (Component) x;
i = (I) x;
}
}
Is there something I can replace the '?
' with that will allow me pass in either an 'A
' or a 'B
'? If I cast 'x
' to a java.awt.Component
and store it in 'c
' and to an I
and store it in 'i
', I lose the benefit of strong typing.
Do I need to declare
class D {
void m(java.awt.Component c, I i) { ... }
}
and call it with 'm(a, a)
' or 'm(b, b)
' where
A a = new A();
B b = new B();
I cannot create an
abstract class C extends java.awt.Component implements I {...}
and pass that in because neither A
nor B
is a C
.
BTW, can this be done in Scala?
EDIT: The actual problem that I am trying to solve is that I have two classes, one that extends JInternalFrame
and another that extends JPanel
. Both are abstract and provide some common functionality for widgets displayed in them (JTables where the user can edit rows, delete rows, etc). The code for editing and deleting rows is always the same, regardless of the underlying object types being displayed. I have several methods that allow the user to click a row, select 'Delete' from a popup menu, and, after asking for confirmation, deletes the selected row and database object, for example. Sometimes I need a frame subcomponent and at other times a panel subcomponent. I have created a delegate class for the common functionality and an instance variable in each of the abstract classes of that delegate type. The JInternalFrame
and JPanel
derived classes then just defer the actual implementations to the delegate. The delegate class, in turn, needs a pointer to the "owner" class for callbacks to get the selected table row, etc., and a pointer to the "Component
" nature of each parent for the JOptionPane
confirmation dialogs.
Using the Java generics approach has worked very well. The delegate class is now defined as a generic class on <T extends Component & DelegateContainer
and each of the owner abstract classes implements DelegateContainer
(where the callback methods are declared).
If I were going to rewrite this in Scala, I would probably do something with traits instead of creating a delegate class. The traits could "add" the delete functionality to the JInternalFrame
derived concrete class, for example.
Thanks for the prompt replies.