llappall
llappall

Reputation: 2952

Java multiple inheritance design pattern?

I have this structure:

Class1

Class2 extends Class1

I have a pile of utility classes on the side that take a Class2 to do all kinds of work.

The problem: I need to create a Class3 extending Class2 to use those classes. Class3 will have a Google API's Android MapView. MapViews can ONLY be created FROM A CLASS EXTENDING MapActivity. I would need

Class3 extends Class2 AND MapActivity.

which can't be done.

Changing my class3 to only extend MapActivity would require me to modify a hoard of code inherited (no pun intended) from previous developers.

Changing my class3 to only extend Class2 breaks because the map requires a MapActivity.

What would be the best approach?

EDIT: Several people mentioned the 'interface solution'. Sounds like the right solution. I forgot to mention, though, that Class2 is an abstract class, and it will be a pain to move the implemented methods around. A lot of code repetition... DRY goes wet on this one... :)

Upvotes: 2

Views: 3266

Answers (5)

Deep
Deep

Reputation: 31

yes Niall C. is right .I am new to this but I khow that Java Doesn't support Mutliple Inheritance of Classes instead this You can create an interface that defines All the methods and members You want to use and Implement it.

Upvotes: 0

havexz
havexz

Reputation: 9590

Can you create Class4 which extends MapActivity and use Class3 as a composition inside Class4. Then you can delegate to apis of Class3. Obviously, this will work if dont need to access some internals of Class3 and above.

If you need access to the internals of Class3, then you can have Class5 extend Class3 to create apis which can help overcome that limitation of not having access to internals.

Upvotes: 0

Pursuit
Pursuit

Reputation: 12345

From what I have seen, the most java-like solution to this problem is as follows.

  1. Generally, try to use interfaces to define types which need to be extended. For example, JTable uses the TableModel interface, which can be implemented from scratch, or by extending AbstractTableModel or DefaultTableModel

  2. Understandably this is not always possible. In your case, you can design Class3 with a private field of type Class1. Then implement as many wrapper methods as required to pass all incoming methods to the internal object.

Of course, (2) will give you all the behavior of Class1, but will not actually be an instance of Class1, which may break something else in your system. Ans the answer to that problem is (again) to make greater use of interfaces.

Upvotes: 1

John
John

Reputation: 3797

Since Java does not support multiple inheritance, you need to make a work around. If I was you I would make an interface that defines all of the things you need and then just extend your Class1

OR

You could simply make Class1 extend MapActivity unless it already extends something else, but you would really only want to do this if Class2 should extend MapActivity. Your Class2 could extend Class1 and your Class3 could extend Class1 and Class1 could extend MapActivity.

Hope this helped!

Upvotes: 2

drum
drum

Reputation: 5651

Instead of having class 3 extending MapActivity, why not try implementing it? I had a very similar problem as yours and solved it by using implements.

Upvotes: 0

Related Questions