An Important link about Generics in Java.
http://www.javabeat.net/articles/33-generics-in-java-50-1.html
what is upper bound in Java Generics?
Ans:
7.1) Upper Bound
The solution to this situation is the usage of wild-cards along with parametric bounding. Have a look over the following declaration.
List animals = new ArrayList();
It tells that a list is being declared with type being anything (?) that is
extending the
Animal
class. Though it looks very similar to the above declaration it has some differences. The first thing is that it is now possible for the animals
reference to point to a list that is holding any sub-type of
Animal
objects.List dogs = new ArrayList();
dogs.add(new Dog()); dogs.add(new Dog());
animals = dogs;
One important difference is that, it is not possible to add elements to the
animals list, though the only exception is adding
null
elements. This is called the Upper Bound for the animals list.
No comments:
Post a Comment