Studyon Minte9.com
Java SE




Generics Wildcard




package testexample; import java.util.TreeMap; public class Test { public static void main(String[] args) { Models<String, ? extends Model> models = new Models(); models.put("A", new ModelA()); models.put("B", new ModelB()); //models.put("C", new ModelC()); // won't work - doesn't extend Model //models.get("A").methodA(); // won't work - cast restrictions ModelA modelA = (ModelA) models.get("A"); ModelB modelB = (ModelB) models.get("B"); System.out.println(modelA.methodA()); // 1 System.out.println(modelB.methodB()); // 2 } } class Models<K, V> extends TreeMap { public V get(String key) { return (V) super.get(key); // only get(key) won't work } } abstract class Model { //abstract protected int methodA(); } class ModelA extends Model { protected int methodA() { return 1; } } class ModelB extends Model { protected int methodB() { return 2; } } class ModelC {}
 


http://jazzjuice.blogspot.ro/2012/01/uses-of-wildcard-types-in-java.html