• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generic Type

 
Ranch Hand
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to avoid casting (  Answer a = (Answer)  foo.calculate();  ) using Generic Type  ?      


 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The same way you do with any generics:-
  • 1: You use a different formal type parameter for the method from the class.
  • 2: You don't instantiate the class as a raw type.
  • 3: You pass something which allows the method to infer its actual type parameter as an argument to it.
  • You are confusing yourself by making the method generic and the class generic and giving them both the same formal type parameter T. Is the return type from that method the same as the actual type parameter of the class? (Probably not.) Why have you made the method generic in the first place? I am pretty sure you don't want both the method and the class parametrised.
     
    kri shan
    Ranch Hand
    Posts: 1491
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Modified the  method and the class parametrized and not working.


     
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    In the immortal words of Charlie Brown, “Good grief!”
    What on earth is that code supposed to mean? Please tell us exactly what goes wrong. For a start, how did you get line 24 to compile?
     
    kri shan
    Ranch Hand
    Posts: 1491
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Gives ClassCastException here ... Customer customer = foo.calculate();
     
    Saloon Keeper
    Posts: 15510
    363
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Well, you're calling foo.calculate() with the type argument Customer. That means that the calculate() method is going to cast the result of findValue() to Customer. findValue() returns Class<Customer>, which is not compatible with Customer, so you get a ClassCastException.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic