Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference between <? extends Object> and <E extends Object>?

Tags:

java

generics

What is difference between <? extends Object> and <E extends Object> ? When should one be used over the other?

like image 874
Vimal Jain Avatar asked Aug 22 '13 15:08

Vimal Jain


People also ask

What is the difference between <? Extends and <?> In generic definition?

<?> and <? extends Object> are synonymous, as you'd expect. There are a few cases with generics where extends Object is not actually redundant. For example, <T extends Object & Foo> will cause T to become Object under erasure, whereas with <T extends Foo> it will become Foo under erasure.

What does extend E mean Java?

extends E means that it is also OK to add all members of a collection with elements of any type that is a subtype of E.

What does extends object mean in Java?

The extends keyword extends a class (indicates that a class is inherited from another class). In Java, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories: subclass (child) - the class that inherits from another class.

What does class <?> Mean in Java?

What Does Class Mean? A class — in the context of Java — is a template used to create objects and to define object data types and methods. Classes are categories, and objects are items within each category. All class objects should have the basic class properties.


1 Answers

Here are some differences that immediately comes to my mind:

  • Type parameter bounds can specify multiple bounds - T extends A & B, but with wildcard you cannot specify multiple bounds - ? extends A & B is invalid.

  • You can have lower bounds with wildcard - ? super A is valid, but not with the type parameter - T super A is not valid.

  • You cannot use wildcard bounds while creating a generic type. You have to use type parameter bounds.

  • Inside a method, if you want some relation between the type parameters of arguments passed, then you have to use type parameter bounds. For e.g, you want to pass two parameterized type with same type parameter. You can't do this with wildcard bounds. So the following method declaration will take two list of same type parameter, that extends Number.

    public <T extends Number> void merge(List<T> list1, List<T> list2) {
    }
    

To end with, I'll add some points from Effective Java - Item 28: Use bounded wildcards to increase API flexibility:

For maximum flexibility, use wildcard types on input parameters that represent producers or consumers. If an input parameter is both a producer and a consumer, then wildcard types will do you no good: you need an exact type match, which is what you get without any wildcards.

Do not use wildcard types as return types. Rather than providing additional flexibility for your users, it would force them to use wildcard types in client code. Properly used, wildcard types are nearly invisible to users of a class. They cause methods to accept the parameters they should accept and reject those they should reject. If the user of a class has to think about wildcard types, there is probably something wrong with the class’s API.


References:

  • Angelika Langer - JavaGenericsFAQ
    • What is a bounded type parameter?
    • What is a wildcard bound?
like image 138
Rohit Jain Avatar answered Sep 23 '22 02:09

Rohit Jain