Let's say I have 2 interfaces, A and B:
public interface A {
List<B> getBs();
}
public interface B {
}
and 2 classes that implement those interfaces:
public class AImpl implements A {
public List<B> getBs() {
return null;
}
}
public class BImpl implements B {
}
Could it possible (maybe using generics) that my getter method returns a list of BImpl typed objects, something as:
public class AImpl implements A {
public List<BImpl> getBs() {
return null;
}
}
Thanks
I think you will need to change the interface declaration to this:
public interface A {
List<? extends B> getBs();
}
However, if you want clients to know which implementation type you use, things will get more complicated:
public interface A<C extends B> {
List<C> getBs();
}
public class AImpl implements A<Bimpl>{
public List<Bimpl> getBs();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With