Java: Method chaining in derived class -
i writing derivedclass (extends superclass) method chaining. example,
public class superclass { protected int responsecode; protected string responsemessage; public void setresponsecode(int code) { this.responsecode = code; } public int getresponsecode() { return responsecode; } public superclass withresponsecode(int code) { setresponsecode(code); return this; } public void setresponsemessage(string message) { this.responsemessage = message; } public string getresponsemessage() { return responsemessage; } public superclass withresponsemessage(string message) { setresponsemessage(message); return this; }
}
and
public class derivedclass extends superclass { protected string credentialtype; protected string credentialid; public void setcredentialtype(string type) { this.credentialtype = type; } public string getcredentialtype() { return credentialtype; } public derivedclass withcredentialtype(string type) { setcredentialtype(type); return this; } public void setcredentialid(string id) { this.credentialid = id; } public string getcredentialid() { return credentialid; } public derivedclass withcredentialid(string id) { setcredentialid(id); return this; } public static void main(string[] args) { derivedclass dc = new derivedclass(); /*dc.setresponsecode(200); dc.setresponsemessage("success"); dc.setcredentialtype("mobileidentifier"); dc.setcredentialid("678882");*/ dc.withresponsecode(200) .withresponsemessage("success") .withcredentialtype("mobileidentifier") .withcredentialid("678882"); system.out.println("derived class: code - " + dc.getresponsecode() + " - response message - " + dc.getresponsemessage() + " - credential type - " + dc.getcredentialtype()); }
}
during compilation of above getting:
derivedclass.java:41: error: cannot find symbol .withcredentialtype("mobileidentifier") ^ symbol: method withcredentialtype(string) location: class superclass 1 error
why getting when credentialtype field in derivedclass , not in superclass? how can chain mixed methods superclass & derivedclass using derivedclass object?
as others have stated, getting compilation error because methods of superclass
return superclass
type, doesn't contain derivedclass
's methods.
the correct way solve make superclass
abstract , use generics on superclass
recursive type (also called f-bound type):
public abstract class superclass<t extends superclass<t>> { // todo attributes, getters , setters public t withresponsecode(int code) { setresponsecode(code); return (t) this; // cast absolutely safe! } // todo other superclass builder methods }
then, can define derivedclass
follows:
public class derivedclass extends superclass<derivedclass> { // todo attributes, getters , setters public derivedclass withcredentialtype(string type) { setcredentialtype(type); return this; } // todo other derivedclass builder methods }
and there won't more compilation errors. downside of approach forces make superclass
class abstract, in order use derived classes.
Comments
Post a Comment