I want to achieve something like this: define an abstract method
public abstract void registerError(*any subclass of an ErrorVO or ErrorVO e);
and in subclass of actual implementation method:
public void registerError(ASubclassOfErrorVO e);
Thanks a lot for your help!
You can use generics in the declaration of the classes like this:
static abstract class Parent<T extends ErrorVO> {
public abstract void registerError(T e);
}
static class Child extends Parent<ASubclassOfErrorVO> {
@Override
public void registerError(ASubclassOfErrorVO e) {
}
}
the <T extends errorVO>
in a defines T
as an class extending errorVO
. The reason this won't work just with method declarations is that you could have many methods in the subclass that could potentially override the parent's method, only by declaring the type T
at the class level does the compiler know which method is being overridden.
public abstract class A <T extends ErrorVO> {
public abstract void register(T e);
}
public class B extends A<SubclassOfErrorVO> {
@Override
public void register(SubclassOfErrorVOe e) {
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.