0

Can some one explains what is difference between those two? From what I researched:

List<List<? extends Object>>: 

we can read any type of object String, Object or any thing else driven from Object class but we can not write to it

List<List<Object>>:

We read and write different type of object which are subclass of Object to it.

To me List<List<Object>> is more useful, what is the use List<List<? extends Object>> over List<List<Object>>?

Waterfr Villa
  • 1,217
  • 1
  • 10
  • 33
  • "More useful" depends on what your use-case is. By the same argument, `Object` is a more useful type than `String`, because you can read and write more different types of thing to it. – kaya3 Dec 17 '19 at 22:07
  • I may sound naive. You can create Object instance but can’t assign if your list has `? extends Object`. Having said that I don’t see the point of use. – Sunil Dabburi Dec 17 '19 at 22:11
  • 1
    `? extends Object` is equivalent to just `?`. A `List>` can take a `List`, a `List` can not. They are different types with different usages and capabilities. You would use a `List>` when you do not care about its contents and do not need to know the type. It can accept all lists. A `List` can not, it can only accept lists over `Object`. It can not take a `List`, because that can ensure that it contains only dogs at compile-time, a `List` cant, they are different. – Zabuzard Dec 17 '19 at 22:11
  • When using generics, you usually want to narrow the scope of your type hints as much as possible. If all the elements in your list will be instances of some class, T, or instances of its subtypes, then you'd write: List> Having a list of raw objects won't give you the benefit of static type checking. – Dylon Dec 17 '19 at 22:15

0 Answers0