a.k.a. “How do I subclass a list element declared in my interface?”
As covered by the rather awesome Ram on Baeldung, the Liskov substitution principle is important and is enforced by Java.
In a current project, we’re looking at our strong types having both interfaces and concrete implementations. There’s a reason for this that’s NOT about making things mockable. (Making everything an interface for mocking is overmocking.)
What’s nice and obvious is that this is possible:
Despite the fact that the
FooImpl subclass returns a subclass of the interface’s declared getter everything compiles happily, because a subclass can be substituted for a base class and still meet the Liskov substitution principle.
However, there’s a problem if you try to do this:
In the Java generic type system,
List<BarImpl> is not a subclass of
List<Bar>. The folks who designed this stuff are pretty clever, and there’s probably a very good technical reason why this is the case.
The question is – can it be solved?
For lots of
Optional of things, this might seem a bit cumbersome, but it is how it’s done… apparently.