Nov 10, 2018

When to use super and extends wildcards in Java Generics

There are two kinds of bounded wildcards in Generics, super and extends. When should you use super wildcard and when should you extends wildcards. Joshua Bloch in Effective Java book has suggested Producer extends, Consumer super mnemonic regarding use of bounded wildcards. If type T is used as producer than use <? extends T>  and if type T represent consumer than use <? super T> bounded wildcards. Bounded wildcards in Generics also increase flexibility of any API.