This week's book giveaway is in the Cloud forum.
We're giving away four copies of Terraform in Action and have Scott Winkler on-line!
See this thread for details.
Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

generic method

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I have following three classes:






When I run Main I get
Exception in thread "main" java.lang.ClassCastException: SuperClass cannot be cast to SubClass
at Main.start(Main.java:28)
at Main.<init>(Main.java:22)
at Main.main(Main.java:18)


I expect that in addSomething the variable t can be cast succesfully to T, so during runtime t should be of type Subclass, but the type is Superclass.
Why? And how to circumvent this?

Cheers,
Y
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're creating a SuperClass object with new SuperClass(). There is no way that can ever be cast to a SubClass reference because it just isn't a SubClass. A cast is telling the compiler "I know you think the type is this, but trust me, it's actually this type". But that still has to be true, or you get the exception.

If what you actually want to do is create an object of type T, then you'd have to use reflection. There'd be no point in doing that with the current example, but you might have to eventually.
 
yusuf Kaplan
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I need to create an instance of T.
But how to do that? Isnt there any other then refeflection ?
How would the code look like?
 
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

yusuf Kaplan wrote:Ok, I need to create an instance of T.
But how to do that? Isnt there any other then refeflection ?
How would the code look like?



I am not sure that this is doable -- meaning I am not sure that, given the current method argument list, there is a way to determine what the type T actually is at runtime.

Henry
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since you can't do new T(); you'll have to use reflection. For that you'll have to pass Class<T> to the method:

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic