*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "generics" Watch "generics" New topic
Author

generics

swaraj gupta
Ranch Hand

Joined: Oct 22, 2010
Posts: 181

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

Joined: Sep 08, 2007
Posts: 3068
    
  33

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.


Mohamed Sanaulla | My Blog
swaraj gupta
Ranch Hand

Joined: Oct 22, 2010
Posts: 181

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

Joined: Sep 08, 2007
Posts: 3068
    
  33

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: generics