aspose file tools*
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


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics" Watch "Generics" New topic
Author

Generics

babai bhaumik
Greenhorn

Joined: May 12, 2007
Posts: 18
I'm facing problem while trying to implement the following code.


While trying this i am getting the error as mentioned above. Error 1 and 2.
Please let me know exactly why, this error is coming. Conceptually where am I wrong?
[ June 10, 2007: Message edited by: babai bhaumik ]

Be bold.
Sasha Ruehmkorf
Ranch Hand

Joined: Mar 29, 2007
Posts: 115
void showTwo(W<? extends Two> o2) //
quote:
My under standing -Now I can pass objects of type Two or anything which extends Two, here it is Three and Four


No, the showTwo()-method takes references of untyped class W or typed references of W<Two>, W<Three> or W<Four> and not of Type Two. Would you ever try to pass this method:
void method(List<Integer> list)
an Integer-object?
[ June 10, 2007: Message edited by: Sasha Ruehmkorf ]
babai bhaumik
Greenhorn

Joined: May 12, 2007
Posts: 18
Hi,
Thanks for the prompt reply.
But on that case what will be the solution?
Best regards,
Bhaumik.
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

Try something like this:



The reason is
- method void showTwo(W<? extends Two> o2) expects a W object of type Two or Three (or, as Sasha said, without type, e.g. a raw type).
- method void showThree(W<? extends Three> o2) expects a W object of type Three (or, again, raw type).


SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
Sasha Ruehmkorf
Ranch Hand

Joined: Mar 29, 2007
Posts: 115
or if you want a method, that can be passed Objects of Type Two or any subtype: You don't need Generics. Just define it this way:
void showTwo(Two t){...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics