0

i know java doesn't support multiple inheritance. I have 3 concrete base classes say A,B and C. I want to reuse all the utility methods in A,B & C in a single class D.I can not use composition as classes A,B and C are not to be played with.Please give me a way out.

Thanks and Regards, Saurabh

Saurabh
  • 93
  • 7
  • 2
    Use interfaces instead. –  Apr 25 '13 at 08:14
  • You may look into this: http://stackoverflow.com/questions/3917190/multiple-inheritance-in-java – DuKes0mE Apr 25 '13 at 08:15
  • Using inheritance just because you want to use methods from some class is a bad idea - that's not what inheritance is meant for. – Jesper Apr 25 '13 at 08:46
  • Inheritance and composition are the ways provided by OO for extending the functionality. I dont understand what you mean by cannot play with A,B and C. You can use them as data members in a different class, say D or E. – techuser soma Apr 26 '13 at 16:58

2 Answers2

0

First of all, using multiple interfaces is possible (and very common).

If order to use multiple extended classes, you need to use:

A extends B, B extends C and D extends A but is not always possible.

Consider using composite pattern instead of multiple inheritances.

BobTheBuilder
  • 18,858
  • 6
  • 40
  • 61
0

I think you can use Facade pattern

Facade http://i.msdn.microsoft.com/dynimg/IC400938.png

As for your example:

  • SubsystemA is your class A

  • SubsystemB is your class B

  • SubsystemC is your class C

  • Facade is your D class, where you can do whatever you want.

I think you can use composition in this way because it doesn't break encapsulation.

bbonch
  • 534
  • 1
  • 5
  • 12