Below code :
List extends String> genericNames = new ArrayList();
genericNames.add(\"John\");
Gives compiler error :
When you use wildcards with extends, you can't add anything in the collection except null. Also, String is a final class; nothing can extend String.
Reason: If it were allowed, you could just be adding the wrong type into the collection.
Example:
class Animal {
}
class Dog extends Animal {
}
class Cat extends Animal {
}
Now you have List extends Animal>
public static void someMethod(List extends Animal> list){
list.add(new Dog()); //not valid
}
and you invoke the method like this:
List catList = new ArrayList();
someMethod(catList);
If it were allowed to add in the collection when using wildcards with extends, you just added a Dog into a collection which accepts only Cat or subtype type. Thus you can't add anything into the collection which uses wildcards with upper bounds.