I have one overarching interface then a bunch of different child interfaces
public interface Parent {
String foo(String s);
}
public interface ChildA extends Parents{
default String foo(String s) { ... }
}
public interface ChildB extends Parents{
default String foo(String s) { ... }
}
Then I have a bunch of spring components that implement these children
@Component
public class GenericChildA implements ChildA {
...
}
@Component
public class GenericChildB implements ChildB {
...
}
Finally I have another service that gets all of these child interface implementations through dependency injection. What I am trying to do is create a more generic function within this Composer class that takes in a List and calls their .foo() method
@Service
public class Composer {
List<ChildA> childrenA;
List<ChildB> childrenB;
public Composer (List<ChildA> childrenA, List<ChildB> childrenB) {
this.childrenA = childrenA
this.childrenB = childrenB
}
public void baz() {
bar(childrenA) // ERROR: childrenA is of type List<ChildrenA> not List<Parent>
bar(childrenB) // ERROR: childrenB is of type List<ChildrenB> not List<Parent>
}
public void bar(List<Parent> parents) {
parents.stream.forEach(parent::foo);
}
}
Is there a way I can get this to work, I'd rather not write an maintain a function for each implementation of Parent?