• 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
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

generics

 
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So many times I have seen such problems from generics saying which statement can be "//inserted here". Though I have knowledge of generics but still I am not able to fix, what is wrong with Line#5 but not with Line#4? Please explain, how should I deal with them:
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 4 is a comment. Are you referring to Line 5 and Line 6?
On Line 6- You are trying to use Integer whereas the class declaration says- <T extends Animal> which means T should extend Animal.
 
swaraj gupta
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mohamed sanaullah wrote:Line 4 is a comment. Are you referring to Line 5 and Line 6?


Oh ! sorry its actually 5 and 6..

What my doubt is: what " <T extends Animal>" has to with the instance creating statements on line no. 5 and 6. Problem is with the instantiation of the class with Integer parameter-type or with the instance variable of type T that could be of type Animal or its subtype? m confused..

Will you please tell me what conclusion have you drawn from: AnimalHolder<T extends Animal> { } only.

On Line 6- You are trying to use Integer whereas the class declaration says- <T extends Animal> which means T should extend Anima


And if am not using T as parameter-type of constructor of class AnimalHolder then what's the problem here.
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

swaraj gupta wrote: What my doubt is: what " <T extends Animal>" has to with the instance creating statements on line no. 5 and 6. Problem is with the instantiation of the class with Integer parameter-type or with the instance variable of type T that could be of type Animal or its subtype? m confused..


Its indicating to the compiler the value for the Type T- So that the compiler can replace T with what ever type you specify. And <T extends Animal> adds a constraint that T has to be a subclass of Animal. I would say- you can brush through the generics concepts again, so that this becomes a bit more clearer.

swaraj gupta wrote: Will you please tell me what conclusion have you drawn from: AnimalHolder<T extends Animal> { } only.


In your class declaration you say- T extends Animal. So what ever type specified while creating the instance of AnimalHolder should be subclass of Animal.

swaraj gupta wrote: And if am not using T as parameter-type of constructor of class AnimalHolder then what's the problem here.


If you are not specifying the Type(T) parameter- Then you arent using generics there and in that case T becomes an Object.
reply
    Bookmark Topic Watch Topic
  • New Topic