Java Generics: Type cast issue (ClassCastException)?

You can try something like this: public > T getSubSet(...){ T subset = (T)this.clone(); subset.clear(); return subset; } creation looks a little funny - feel free to change it to whatever you want :) As a bonus you will not need to cast :) paramsSubSet = params. GetSubSet(...).

Thank you! I actually tried something very similar to this, but was only casting to T on return, rather than creating a new instance of T (as the other commenters have pointed out)... It all makes sense now. Thanks, everyone!

– flux Nov 16 '10 at 15:53.

Your Problem is that the Subset returned by getSubSet is a of instance SomeCustomMap and not of Parameters. This problem does not deal with generics. You will get the same problem if you did not use generics.

I don't know how you create an instance of subset but maybe you could use the template desing pattern and some generics to fix your problem.

Though I've commented asking for more information, based on what you've posted so far, I think getSubSet is constructing a SomeCustomMap to return (with new SomeCustomMap) somewhere. If you don't override getSubSet in Parameters, then Parameters. GetSubset will return a SomeCustomMap (the base class), not a Parameters, so your typecast to Parameters fails.(Hot tip, if you override getSubSet in the Parameters class, you can change the return type to Parameters and avoid the typecast.).

Yes, you are correct in that I am doing a someSubset = new SomeCustomMap() and filling this subset with the data from the parent (this). And for now, say I do not have any overrides in the Parameters class; Is there any way this can be done without the override? – flux Nov 16 '10 at 15:39 I don't know what the best way is.

I see you accepted an answer that uses clone() already, but you could also use methods on this.getClass(), or you could factor out a (small) protected function whose only purpose is to construct an object of the right type, and override that. – Ken Bloom Nov 16 '10 at 19:43.

Generics don't inherently have anything to do with casting (save that due to the nature of erasure, generic parameters cannot be checked during a cast). If you're getting a ClassCastException in this case, it means that the object returned really is not an instance of Parameters. Just before you cast, try calling System.out.

Println(params. GetSubSet(...).getClass()); and see what the actual run-time class of the subset is. Chances are the problem lies elsewhere, as your expectation that the subset is a Parameters object is almost certainly not correct at runtime - it's a SomeCustomMap or some other subclass thereof.

As others have explained, the issue is that the actual object you are constructing in getSubSet() is not an instance of Parameters. Here's one possible workaround. I don't love it, but it is a way to declare the method in SomeCustomMap but have its return value be typed correctly for any subclass.

Public static > getSubSet(T fullSet) { T subset; ... (use fullSet instead of this) return subset; }.

I'm having trouble figuring out how to properly cast a generic object in java to a type that extends the generic object. Running code similar to the above consistently throws a ClassCastException, the likes of which I do not fully understand. Any assitence for how to correctly set up a scenario similar to the above would be appreciated!

Namely, how might I properly cast the the SomeCustomMap object that is returned from the params. GetSubSet(...) method back to a Parameters object? Thanks in advance!

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions