I know Java forbids inheriting from multiple classes and allows implementing any number of interfaces. However, while interfaces are good for polymorphism, they cannot contain any actual code that their subclasses may want to share. What if I have two different superclasses that have shared code that I want to use in my subclass?
Example: In my program, there are two superclasses, one that works with HashMaps and one that works with Strings, and both have direct subclasses, and all is fine there. But now I have a third that requires the shared functions of both superclasses, but it can only extend one. Is there an easy way to redesign this class structure without having to duplicate a lot of code?
I’ve seen this done by
Creating a class that implements the interfaces, then delegating all calls to the actual objects
This will not give you access to the protected members in the class heirarchy. If that is what you are looking for, you’re out of luck in Java. However, this will allow you to refer to this object as both IFirst and ISecond using polymorphically. Thus, you’ll be able to access it in external code that requires either one of the two types.
Note: I stole the naming from JB.