I have the following interface heirarchy (with all non-relevant functions stripped out). I am getting this error when trying to compile it:
types ValidLineGettable and ValidateValue<java.lang.String> are incompatible; both define getObjectCopy(), but with unrelated return types
This is all derived from the same function--not two different functions with the same name, the same function in the same interface. How do you deal with interfaces that must inherit from two different interfaces that themselves must inherit from a singular base interface?
There are two SO questions I've found regarding different functions that happen to have the same name
- Inherit method with unrelated return types
- Java - Method name collision in interface implementation
In my case it's the same function, both conceptually and in name.
(Although I am interested in opinions on if the Copyable
interface is a good idea to begin with...it's in a lot of code that I use, and has worked well for me...I am mostly interested in the general inheritance/design question.)
I am not clear on how to best deal with this. I would appreciate any advice. Thank you.
public interface Copyable {
Copyable getObjectCopy();
}
interface ValidateValue<O> extends Copyable {
//Other functions...
@Override
ValidateValue<O> getObjectCopy();
}
//For classes that may be able to be Decorated into a TextLineValidator
interface ValidLineGettable extends Copyable {
//Other functions...
ValidLineGettable getObjectCopy();
}
interface TextLineValidator extends ValidateValue<String>, ValidLineGettable {
//Other functions...
@Override
TextLineValidator getObjectCopy();
}
The error:
C:\java_code\Copyable.java:17: types ValidLineGettable and ValidateValue<java.lang.String> are incompatible; both define getObjectCopy(), but with unrelated return types
interface TextLineValidator extends ValidateValue<String>, ValidLineGettable {
^
1 error
Tool completed with exit code 1