If
new ArrayList<?>(); // is not Legal
And if
new ArrayList<? extends Number>(); // is not Legal,
Then why is
new ArrayList<Set<?>>(); // IS Legal
Can someone explain with example.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This happens, because in your concrete type declaration you’re using interface as generic parameter. Think about how it would be used in the future:
In first line – you’re defining a list of
ArrayListtype, that will be holding ‘some set’ (of any type). Then, in second line you’re declaring and defining set of concrete typeHashSet. It is casted intoSet<?>so there is no problem with adding it to previously created list.