I’ve recently had to break a simple class down into two versions, a Legacy version for older client versions, and a newer version that has migrated to a separate interface.
As there is much common code, I split this down into an abstract class with 2 concrete classes. With a structure as in the below:
interface ParentInt {
// Common methods
}
interface ChildIntA extends ParentInt {
// Legacy methods
}
interface ChildIntB extends ParentInt {
// New model methods
}
abstract class AbstParentClass implements ParentInt {
// ...
}
class LegacyConcreteClass extends AbstParentClass implements ChildIntA {
// ...
}
class NewConcreteClass extends AbstParentClass implements ChildIntB {
// ...
}
What I’m wondering is whether there are any pitfalls I might encounter because AbstParentClass implements ParentInt and the two concrete classes also implement children of this interface? Might there be a better pattern for this situation?
My code currently all works regardless of whether AbstParentClass has this implements directive or not. In fact, because the two concrete classes are instantiated separately in different threads, AbstParentClass is never actually referenced directly anywhere else.
In my situation the interfaces are part of an API and as such are unchangeable from my POV.
There are no problems with this approach. I understand your concern about the apparent “multiple inheritance”, but the way that Java handles interfaces ensures that this kind of thing is never actually an issue. Interface methods with the same signature are “fused” and only one implementation method is allowed or required, regardless of how many identical interface methods are inherited.