Thursday, October 11, 2012

Java Generics II

There are so many things broken for java generics. I don't know why java generics is so half baked.
Here is what I wanted to do
Class X<Type>{

public <T extends SomeOtherType> X<T> copy(T param) {
   // do something
   return copy of X
}

Class Y<Type> extends X {
@Override
public <T extends SomeOtherType> X<T> copy(T param) {
   // do something
   return a copy of Y
}
Well, technically my requirement is simple and I see no harm in that. I was just trying to implement a copy method for a class which takes generic parameter. Then I create a subclass of that and trying to override the copy function; guess what? If I use Override annotation, it says no such method exists in base class, if I don't use Override annotation it says name conflict!

Update:
Well, I found the problem; the second class should be written as follows
Class Y<Type> extends X<Type> {
@Override
public <T extends SomeOtherType> X<T> copy(T param) {
   // do something
   return a copy of Y
}

huh! I don't know who is the culprit me, or java generics!

No comments:

Post a Comment

Please, no abusive word, no spam.