Any class that extends an interface must implement the methods declared in the interface. Not sure if this is possible but what I want to do is the following :
You can do this with generics, but it's not enforced at runtime. You should also be using implements
to implement an interface in a class, rather than extends
.
interface Test {
T get();
}
class A implements Test {
int val;
A(int x) {
val = x;
}
@Override
Integer get() {
return Val;
}
}
class B implements Test {
String val;
B(String x) {
val = x;
}
@Override
String get() {
return Val;
}
}
Generics only apply to classes, so you're forced to use Integer
rather than int
as the return type.