Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics

 
babai bhaumik
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Sasha Ruehmkorf
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks for the prompt reply.
But on that case what will be the solution?
Best regards,
Bhaumik.
 
Sergio Tridente
Ranch Hand
Posts: 329
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Sasha Ruehmkorf
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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){...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic