Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

generics

 
swaraj gupta
Ranch Hand
Posts: 182
C++ Java Oracle
  • Mark post as helpful
  • send pies
  • 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:
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • 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: 182
C++ Java Oracle
  • Mark post as helpful
  • send pies
  • 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
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic