I want to restrict the type passed to the method as a parameter below:

public <T super Number> void fill(T list) {} 

But IDEA stresses me that you can't do that.

Although when I do the same restriction from above like this:

 public <T extends Number> void fill(T list) {} 

Then everything works fine.

What is the difference? Why can I put a restriction on top, but I can’t set the same from the bottom? How can I achieve the desired effect using exactly <T ... > , and not <? ... > <? ... > ?

    1 answer 1

    If we write <T super Number> , then we can have any type T in the inheritance hierarchy of the Number class. Moreover, we do not know anything about these classes except that they are inherited from Object . Accordingly, it’s impossible to call any methods directly on an object of type T

    The situation with <T extends Number> opposite. We still have no idea what class T , but we absolutely know that it has methods that are defined in the Number class, since class is inherited from Number .

    Therefore, the construction of the form <T super Number> is meaningless. The main profit of its use is application with parameterized classes, for example List<? super Number> List<? super Number>