• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Generics

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends..

i am really confusing with this generics,

see the below example

class C{}
class A extends C{}
class Impl {
void method(){
ArrayList<? extends C> arl = new ArrayList<A>();
arl.add(new A());//compile time error;
}
}

why its giving compile time error , already arl is type A arrayList means we can store the A class Objects, right?...

really i am confusing this senario.. pls help me the same.....
please give me detail explanation

ok
thank you
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I think that is not allowed becoz it compromises type safety. If it is allowed, i can create a and then and that would break type safety. Exercise caution while using wildcards.
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you use extends to denote a type parameter bound, you are not requiring a
subclass-superclass relationship, but merely a subtype-supertype relationship. It is also important to remember that the bounded type does not need to be a strict subtype of the bound; it could be the bound as well.

In other words, for a Collection<? extends Number>, you could assign a Collection<Number> (although Number is not a strict subtype of Number) as well as a Collection<Integer>, Collection<Long>, Collection<Float>, and so on but you can not assign a Collection <Object> even though Object is a superclass of Number
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider piece of code with comments.


Read the point 4 (about wildcards) of the generics tutorial - there is a good and quite detailed explanation of this situation.
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is because at runtime, there is no difference between a generics collection and a non-generics collection. (Type erasure)
ArrayList<? extends C> arl
is as good as
ArrayList arl
at runtime.
if there is no compile time error, then anything can be added to collection at runtime which will break the type safety. So normally you will no be able to assign a ArrayList<C> arl = new ArrayList<A>,
but the wildcards '?' allow you to do the assignment and with extends it can be read only, you can't do structural modifications.

consider the example



Hope this will clear your doubt.
 
And then we all jump out and yell "surprise! we got you this tiny ad!"
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic