• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Problem when creating a list with generics.

 
Ranch Hand
Posts: 40
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi falks,
Please note that there is a class called Dog that extends another class Called Animal.
if I create a ArrayList as below, That shows a compilation error at line 2. What is the reason for that?

Thanks.
 
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what if suppose you are working on big project and someone put Horse object inside this list then? because he knows Horse extends Animal (suppose class Horse extends from Animal ). Generics is use for compile time protection. And you may think that you have a list which contains only dog.
 
Buddhika Mawella
Ranch Hand
Posts: 40
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That means just after I assigning my list of dogs to List<? extends Animal> list,
That list becomes read only. I mean we cant add Dogs,Horse or Animals into list. Am I correct?
 
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can still add null, but nothing else.
 
Buddhika Mawella
Ranch Hand
Posts: 40
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But still I feels like its unfair. Because when I am creating my list I am initializing it as, new ArrayList<Dog>(); which says I am creating an array list that can hold Dog objects.Then why cant I add Dog objects?
 
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Buddhika Mawella wrote:But still I feels like its unfair. Because when I am creating my list I am initializing it as, new ArrayList<Dog>(); which says I am creating an array list that can hold Dog objects.Then why cant I add Dog objects?



Because your variable does not tell this. The type check is done on the type of the variable, not the type of the object. If you don't tell the compiler that you hold a list of dogs, it doesn't know this.
 
Buddhika Mawella
Ranch Hand
Posts: 40
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, thanks mate.
 
Destiny's powerful hand has made the bed of my future. And this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic