• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Generics ? extends

 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Could you please let me know what is happening here..I am getting confused.
 
Saloon Keeper
Posts: 13280
292
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic