java – Nested generics

Question:

Good day, I decided to go deeper into generics than SomeClass<T> , and ran into difficulties.

let's say there is a class

public class SomeClassA<T>{

}

and class

public class SomeClassB<X>{

  public void someMethod(Object, X){}

}

How can I set generic in SomeClassB so that it has <SomeClassA<T>> on the input and works, accordingly, with T

Thanks in advance

Answer:

Perhaps you meant inheritance:

public class MyClass {

    public static void main(String[] args) {
            SomeClassB<Integer> scb = new SomeClassB<>();
            scb.print(10);
        }
    }

    class SomeClassA<T> {
        public void print(T obj) {
            System.out.println(obj.toString());
        }
    }

    class SomeClassB<T> extends SomeClassA<T> {
        public void print(T obj) {
            super.print(obj);
        }
    }
}
Scroll to Top