How can I apply generics in Java having nested bound type parameters?

40 Views Asked by At

I have few classes:

class A [ Base class ]
    class B1
         class B11
         class B12
    class B2
         class B21
         class B22

I want to use generics like this

A<B1<B11>>> a1 = new A<>();
A<B1<B12>>> a2 = new A<>();
A<B2<B21>>> a3 = new A<>();
A<B2<B22>>> a4 = new A<>();

So my class A is something like this

Class A<T> {
private T t;
Getter and Setters...
}

Above class A can take T as B1<B11> or B1<B12> or B2<B21> or B2<B22> type.

Class B1<T> {
private T t;
Getter and Setters...
}
Class B2<T> {
private T t;
Getter and Setters...
}

Above Class B1 will take T as B11 or B12 type and Class B2 will take T as B21 or B22 type.

Note: Cannot use only B1 or B2 because of code readebility.

Question 1. Is it possible in Java using generics? If yes, then how can I achieve this.
Question 2. Can we apply inheritance for above case and also applying generics with that? If yes, then how can I achieve this.

Thank you. Any help is highly appreciated.

0

There are 0 best solutions below