Tuesday, 13 August 2013

Java Abstract Classes: Returning "this" pointer for derived classes

Java Abstract Classes: Returning "this" pointer for derived classes

I am currently trying to write some custom exceptions with helper methods
for setting the variables like this:
public class KeyException extends RuntimeException {
protected String Id;
protected KeyException(String message) {
super(message);
}
protected KeyException(String message, Throwable cause) {
super(message, cause);
}
public String getId() {
return keyId;
}
public KeyException withId(final String Id) {
this.Id = Id;
return this;
}
}
However, in my derived classes, I cannot use the "withId" method as it
only returns the base class - is there anyway to return the "this" pointer
without having to override the method in every single derived class?

No comments:

Post a Comment