1

I need to mimic multi inheritance in Java. It might be wrong design but, since my problem is not with (parent) class functionality, I have not been able to mimic the feature with interfaces. Here is more description on the problem. I have a class called AbstractModel.java with number of methods. On the other hand, there is another class AbstractTableModel.java. Now assume that there is a class called table controller.java that deals with AbstractModel.java and on the other hand there is class called Tableview.java that deals with AbstractTableModel.java. I need have to some way to define : public class A extends AbstractModel, AbstracTableModel so that both view and controller can use the same class with extension. Please note that the solution AbstractTableModel.java extends AbstractModel is not a solution since it is a built-in java class.

Thank you.

bharath
  • 14,283
  • 16
  • 57
  • 95
Hasti
  • 239
  • 1
  • 3
  • 17
  • 1
    Can't you make AbstractModel and AstractTableModel interfaces, and then make class A implement those interfaces? – Asterisk Nov 28 '11 at 06:14
  • See also this example of a [`SharedModel`](http://stackoverflow.com/a/7572903/230513). – trashgod Nov 28 '11 at 06:15
  • You can convert 1 class: AbstractModel class or AstractTableModel class into interface, then class A extends 1 class and implements remaining interface. – Thinhbk Nov 28 '11 at 06:18
  • 1
    since there are many other classes extending the AbstractModel. By making that an interface I have just got very much redundancy by copying the same code in all the children. Abstract table model is java built-in class under javax.swing.table.AbstractTableModel; – Hasti Nov 28 '11 at 06:21
  • In other words if Class A implements (AbstractModel/AbstractTableModel) as an interface it can not be passed to controller or view accordingly. – Hasti Nov 28 '11 at 06:34
  • 1
    What do you mean by "redundancy be copying the same code in all the children"? If you make an Interface for AbstractModel (which is a matter of less than a minute in Eclipse), there is no need to change any of the children, and all methods anywhere else (in the workspace) that before used an `AbstractModel` can in the same step automatically be changed to accept the interface instead. – arne.b Nov 28 '11 at 07:20

2 Answers2

1

You cannot do it directly in Java but you can use delegation design pattern. Look here. It is a very similar question on SO.

Community
  • 1
  • 1
gigadot
  • 8,879
  • 7
  • 35
  • 51
  • hanks @gigadot I think I was not clear enough on my question. As I mentioned before my problem is not functionality. to Summarize: There is a method in view that take AbstracttableModel. i.e. display(AbstractTableModel model) and there is a method in controller that accepts Abstract model i.e. addModel(AbstractModel model). Now how I can add the same model to the controller and display on the view without multiple inheritance? One way that came to my mind was to create a second class that has two members: AbstractModel AM, AbstracTableModel ATM and Pass AM to the controller and ATM to the view – Hasti Nov 28 '11 at 13:55
  • it is not possible to create a new class that subclass both AbstractModel and AbstracTableModel. If you want to change your controller and view methods' signature just as you described to accept this new class, you are better redesign your model representations. – gigadot Nov 28 '11 at 14:30
  • Do you know of any particular design pattern that deals with this issue at all other than delegation design pattern for functionality? – Hasti Nov 28 '11 at 22:39
0

Change your code that expects an AbstractTableModel so that it accepts a TableModel instead (this is an interface implemented by AbstractTableModel). This should be done anyway. Never depend on an concrete implementation when you can instead depend on an interface.

Then you can easily let class AbstractModel implement TableModel.

You should probably also think about creating an interface for AbstractModel. Usually one would create an interface X, an abstract base implementation AbstractX which provides common functionality shared by all implementations, and implementations MyX, YourX etc. Only the concrete implementations (but not necessarily all of them) would know and depend on AbstractX, but no code outside this hierarchy.

The Java Collections Framework is a good example of this: There is for example List, AbstractList, ArrayList and LinkedList, but there is no code that actually depends on an AbstractList. Most users of a list just use any List, or if they need a concrete implementation for some reason, they depend on that.

Philipp Wendler
  • 11,184
  • 7
  • 52
  • 87
  • I do not think `List` and `AbstractList` are a good comparison here. The latter has no public methods other than those inherited from the interface, while `AbstractTableModel` inherits less than half of its public methods from `TableModel`. Also, I see dozens of classes extending `AbstractTableModel`, but only two directly implementing `TableModel` (in a third-party library), so there is a lot of code actually depending on this one implementation rather than the interface. – arne.b Nov 28 '11 at 07:08