Search code examples
javaandroidinheritancecompositionbase-class

Runtime determination of base class


I have two OS dependent base classes - Class A and Class B in the form of separate jars.

Before N OS, the behavior was --> Class C extends Class A.

Now, the behavior that is required is:

Till Android M: Class C extends Class A.

For Android N and above: Class C extends Class B.

What would be the most efficient way of doing this?


Solution

  • Come up with a common interface that covers the functionality that A and B provide, and write adapters between this interface and the two said classes. Then compose the appropriate adapter into your C class.