Let's consider a scenario where we have an abstract class A and three concrete classes that inherit from it: A1, A2, and A3. There is also another hierarchy tree with an abstract class B and three concrete classes B1, B2, and B3. Each concrete class A needs to hold a list of objects of the corresponding B class. For example, A1 holds a list of B1 objects. What would be the optimal solution for this situation:
One approach could be to make A a generic class and have it hold a generic list of T which extends B, like so:
abstract class A<T extends B> { protected arr: Array<T extends B> }
This way, we can declare each A class as follows:
class A1 extends A<B1>
Alternatively, we can use composition in the following manner:
class A1 extends A { private arr: Array<B1> }