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

Generics questions about ? extends Type

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi people. I am wrecking my brain on this issue about generics and ? extends type. My reference for the question is the Khalid&Rassmussen book for the SCJP6, questions 14.15. We've got the following classes:





Why in the world is it not possible to *put* any values into *g* ?

Thanks for any replies
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This concept is quite simple once you got your head around it...

Garage<? extends Car> means the compiler expects any type of Garage that is either car or a subtype of car.

Now, the compiler only knows of Car. And at runtime you pass it a Garage<Audi>. And now you try to set a Car to the with Audi parameterized type. KABOOOOOMM.

Got it ?
 
Sheriff
Posts: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Many people are confused at this. See if this could clear your doubt...
 
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you read this thread?
 
Ankit Garg
Sheriff
Posts: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Monu Tripathi wrote:Have you read this thread?



I was first
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ofcourse you were!

There was one more thread where we had a discussion about <?>, <? super > and < ? extends>. I was looking for that thread but the guy who asked the question misbehaved and apparently, the thread was deleted. Bummer!



 
Oddbjorn Lona
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys... I'm still confused though ... :-(


Garage<? extends Car> means the compiler expects any type of Garage that is either car or a subtype of car.

Now, the compiler only knows of Car. And at runtime you pass it a Garage<Audi>. And now you try to set a Car to the with Audi parameterized type. KABOOOOOMM.



Its this that confuses me. Since you've already specified you will only add types of car and subclasses, it should be allowed to add these types. But you're not.

I realise that when we get the objects back it will be of car or its supertype.

And because of this, I would think we should be able to add object, vehicle and car.

Its sort of a catch 22 ...

Is this just a mechanism for being able to tell what we are getting when we use "get"?
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Oddbjorn Lona wrote:

... Since you've already specified you will only add types of car and subclasses, it should be allowed to add these types. But you're not.



Note that you have just specified the UPPER BOUND on type and not the actual type that will be passed. The actual type will only be known only at run time. GenericType<Car> is not same as GenericType<Sedan>. If at runtime you use GenericType<Sedan> you cannot add a Car object to it. The cast will fail.
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I would think we should be able to add object, vehicle and car.



How can you add object,vehicle and car if the object made at runtime is a specific vehicle called subaru or honda? If it is <? extends Type> means the particular collection can have the type or its subtypes but you cannot add anything as the compiler isn't sure what the type safe object at runtime.
 
You can't have everything. Where would you put it?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic