0

I tried to run the below code

List<? extends Object> list = new ArrayList<Integer>();
list.add(1); //compile error
list.add("abb");//compile error

I understand we can't add anything to above list. Then what is the purpose of creating a list in this way ?

What is the reason java allowed us to create a list this way ?

Thanks

Manish
  • 1,274
  • 3
  • 22
  • 59
  • Wildcards should only really be used in method parameters to make the API accept a broader range of inputs. You should only rarely use them as local variables or fields. – Andy Turner Jul 03 '18 at 18:06
  • "I understand we can't add anything to above list." Not true, you can add `null`. – Andy Turner Jul 03 '18 at 18:07

0 Answers0