Saibabaa Pragada wrote: //How we can extend String. String is a final class know ?
We're not extending String. We're just declaring list to be a List that can hold Strings, and theoretically stuff that extends String (even though such objects can't exist).
//Syntax error on token(s), misplaced construct(s)..We are able to assign at Line # 7 but not here..
You are using wrong syntax here. You can't assign a class to an object. It's like saying: Object o = String. It's wrong syntax. There's more wrong with this particular line, but I'll go into that in the next part:
//Cannot instantiate the type ArrayList<? extends String>
You can't instantiate ArrayList<? extends String> because it's not a valid class. You can only use wildcards in variable declarations, not in concrete classes.
Let's compare this to the use of interfaces.
You could say CharSequence sequence = "A string", right? We are assigning an instance of the concrete class String, to a variable declared as a CharSequence.
In a similar way you could say ArrayList<? extends Number> list = new ArrayList<Integer>(). We are now assigning an instance of the concrete class ArrayList<Integer>, to a variable declared as an ArrayList<? extends Number>.
However, we can NOT make new instances of ArrayList<? extends Number>, because it's not a concrete class, just like we can't make new instances of CharSequence, or Number.
//Compile Error : The type Object is not generic; it cannot be parameterized with arguments <List<? extends String>>
This should be obvious. Object is not a generic class, so you can not apply generic arguments to it.
Drove my Chevy to the levee but the levee was dry. A wrung this tiny ad and it was still dry.