• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Generics

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thanks for the prompt reply.
But on that case what will be the solution?
Best regards,
Bhaumik.
 
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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){...

 
reply
    Bookmark Topic Watch Topic
  • New Topic