Generics - lower/ upper bound wild card behaviour?

Viewed 992

I am trying to understand the behavior of lower as well as upper bound wild cards.

Got an issue while trying to compile the below code.

Collection<? extends Object> c = new ArrayList<Object>();
c.add(new Object()); // Compile time error

For figuring out the issue, I simply tried the lower bound wild card as well. Fortunately or unfortunately, code compiles fine but created so many confusion.

Collection<? super Object> c = new ArrayList<Object>();
 c.add(new Object()); // Compiles fine

Can somebody explain me, how these two code snippets are working. It would be great if someone can provide additional examples/links.

Please correct me if I did something wrong above.

Thanks in advance.

2 Answers
Related