Java Multiple inheritance, get rid of duplicate code
java, multiple-inheritance
Solution
You could use composition:
- create a new class MyCommon with the common code
- add an instance of MyCommon in MyB and MyC and delegate the work to MyCommon.
Problem
There are library classes B and C, both inherit from class A. I have 2 classes that extend B & C, namely MyB & MyC. ``` A / \ B C / \ MyB MyC ``` `MyB` & `MyC` share lots of common code and they are only slightly different. I want to get rid of duplicate code, how can I do this in java? In C++ it would be possible by creating a common base class and putting everything that is common in it as follows: ``` A / \ B C \ / MyBase / \ MyB MyC ```