I'm trying to override a method that returns an instance of the class that it is declared in, which changes the parameters of the class between the instance that the method is called on and the instance returned by the call. I would like to be able to return an instance of the Overriding_Attempt
class from the overriding method.
It's not clear to me why Overriding_Attempt<X>
is not equivalent to DuoClass<X,ContainerClass<X>>
given the extension in the declaration of Overriding_Attempt
. I would like to be able to return this more specific instance of DuoClass<X,ContainerClass<X>>
. The error I get is The return type is incompatible with DuoClass<A,ContainerClass<A>>.methodToOverride()
This issue is very much related to the Nested Parameters question posted here.
Also of note: Deleting the declaration of parameter Y from Overriding_Attempt
seems to cause a signature mismatch such that the error becomes Name clash: The method methodToOverride() of type Overriding_Attempt<A> has the same erasure as methodToOverride() of type DuoClass<A,B> but does not override it
. It seems odd that I would need to declare an unused parameter but that's not my main problem.
public class SomeClass {
}
public class ContainerClass<A extends SomeClass> {
}
public class DuoClass<A extends SomeClass, B extends ContainerClass<A>> {
public <X extends SomeClass, Y extends ContainerClass<X>> DuoClass<X,Y> methodToOverride(X field) {
return null;
}
}
public class Overriding_Attempt<A extends SomeClass> extends DuoClass<A,ContainerClass<A>> {
//Error Here: The return type is incompatible with DuoClass<A,ContainerClass<A>>.methodToOverride()
public <X extends SomeClass, Y extends ContainerClass<X>> Overriding_Attempt<X> methodToOverride(X field) {
return null;
}
}