• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

generics Strike again !!!

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator





The answer is error at line 2. WHy?
Doesn't the decalaration means that we can add any supertype of String class?

 
Sheriff
Posts: 9643
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No! The declaration means that the list is of type String or a super type of String. But you don't know what it is. So you can't add elements of super class of String to it. But since the type of the list is a super type of String, so one thing's for sure, we can add elements of type String to it as a String object will always be assignable to super type of String...
 
Prav sharma
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. Does that mean If its < ? super TYPE > , then only objects of type < TYPE > can be added? Nothing else?



Ankit Garg wrote:No! The declaration means that the list is of type String or a super type of String. But you don't know what it is. So you can't add elements of super class of String to it. But since the type of the list is a super type of String, so one thing's for sure, we can add elements of type String to it as a String object will always be assignable to super type of String...

 
Ankit Garg
Sheriff
Posts: 9643
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Prav sharma wrote:Ok. Does that mean If its < ? super TYPE > , then only objects of type < TYPE > can be added? Nothing else?



Both TYPE and sub-types of TYPE can be added to the collection. And Prav please don't use unnecessary formatting like colors or font-size ...
 
Prav sharma
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it that way? Even sub - types of type <TYPE> ???

Ankit Garg wrote:

Prav sharma wrote:Ok. Does that mean If its < ? super TYPE > , then only objects of type < TYPE > can be added? Nothing else?



Both TYPE and sub-types of TYPE can be added to the collection. And Prav please don't use unnecessary formatting like colors or font-size ...


 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, subtypes of type also.

 
Prav sharma
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok.
Then if we have <? extends TYPE> , we can't add anything.
Then whats the use of using this ? extends TYPE syntax.

Punit Singh wrote:Yes, subtypes of type also.

 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<? extends TYPE> and <? super TYPE> used on the RHS and used to restrict LHS list parameter type means <TYPE> on LHS side.
so
List<? extends TYPE> restricts LHS to be <subtype of TYPE or TYPE> means:
List<? extends Number> list=new ArrayList<Integer>();//ok
List<? extends Number> list=new ArrayList<Number>();//ok
List<? extends Number> list=new ArrayList<Double>();//ok
List<? extends Number> list=new ArrayList<Float>();//ok

List<? extends Number> list=new ArrayList<Integer>(); valid as Integer extends Number. Same way

so if we do list.add(new Double(10.0)); then think what will happen, List<? extends Number> allows ArrayList<Integer>().
so finally what happened:

new ArrayList<Integer>().add(new Double(10.0));//not valid

so compiler will make you safe by not allowing this type of gotchas.

But if you say
List<? super Number> list=new ArrayList<Number>(); // Number is super of Number
List<? super Number> list=new ArrayList<Object>(); // Object is super of Number
List<? super Number> list=new ArrayList<Integer>(); // Integer subtype of Number so not valid.

Now suppose
you have
List<? super Number> list=new ArrayList<Object>();
list.add(new Double(10.0));
means
new ArrayList<Object>().add(new Double(10.0));// valid no problem

Got ?
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Prav sharma wrote:Ok.
Then if we have <? extends TYPE> , we can't add anything.
Then whats the use of using this ? extends TYPE syntax.

Punit Singh wrote:Yes, subtypes of type also.



The use comes where you want your user to read only the contents of different types of list, you do not want your user to add any content in your list.
 
Prav sharma
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok !!!

Means if i have lists ( each of different type and i want users to read them via a common function without able to add anything.

Great !!! Correct if wrong !!!

Punit Singh wrote:

Prav sharma wrote:Ok.
Then if we have <? extends TYPE> , we can't add anything.
Then whats the use of using this ? extends TYPE syntax.

Punit Singh wrote:Yes, subtypes of type also.



The use comes where you want your user to read only the contents of different types of list, you do not want your user to add any content in your list.

 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This method will allow you to pass List<Number>, List<Integer>, List<Float>, List<Double>, List<Byte>, List<Short>, List<Long>, so multipurpose method.

Any doubt now?
 
Prav sharma
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it.

Thanks alot !!!

Punit Singh wrote:

This method will allow you to pass List<Number>, List<Integer>, List<Float>, List<Double>, List<Byte>, List<Short>, List<Long>, so multipurpose method.

Any doubt now?

 
Tick check! Okay, I guess that was just an itch. Oh wait! Just a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!