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

Question on Generics Wild Card

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
List<? extends Animal> genericExtends = new ArrayList<Animal>();
This is not allowing me to add anything to it then why allow to instantiate a list .
 
Ranch Hand
Posts: 45
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


"Says this reference can be initialized with Animal or anything that extends Animal e.g. CAT, DOG, etc. This was about initialization.

But once initialization is done, compiler has no idea what this reference is initialized to, It can be anyone from CAT, DOG, or Animal. Since It is not sure that what object type is this, compiler gives compilation error.



 
Thiyagarajan Kamala
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not let me add anything that is Animal or sub type of Animal I have already told the compiler it is referring to Animal.

Apprecicate your reply.

 
Balraj Momi
Ranch Hand
Posts: 45
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My Friend, object creation happens at Runtime not at Compile time.

Read this Generics Super and Extends

you will get full insight what I was trying to say. Go through this, then come back If you have any question on this.
 
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

Thiyagarajan Kamala wrote:List<? extends Animal> genericExtends = new ArrayList<Animal>();
This is not allowing me to add anything to it then why allow to instantiate a list .



There is use of this indeed. Suppose you have this structure



So the thing to say is, you cannot add any element to such a list, but you can definitely get elements out of it. So it can be useful if the list is already populated...
 
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
Remember:
  • Generics was introduced for type safety, so that you can detect and prevent "possible" runtime errors at compile time.
  • Also, compiler adds required casts whenever necessary so, no explicit casting is required.


  • When you write this statement, you are saying that genericExtends can be assigned to any List-type which holds Objects of type Animal or its sub classes. So, the only thing you are guaranteeing to the compiler is that the UPPER BOUND of the type in the list will be Animal.

    Compiler cant allow you to add any object to this list, because it does NOT know the actual type of List that you assign at Run time. Add operation will have the risk of ClassCastException. For e.g.:

    Compiler will allow you to get/read an object from this list, because it knows that the object returned will be atleast of type B or Animal(in your case). So the cast will never fail.

    Hope that helps...

    Read this for more info:
    Sun Tuts: Wild Cards
    Generics : What is a wild card
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic