inheritance - How to populate the fields of a subclass based on a superclass in Java? -
i have base summary class below:
public class summary{ private string name; private string status; private string id; // getters , setters }
i extended class customer summary:
public class customersummary extends summary{ private string lastlogin; private string address; // getters , setters }
now calling rest endpoint maps response summary object. need set lastlogin , address calling rest endpoint , return combined data customersummary object.
summary summary = restclient.getstatus("1234"); customersummary customer = new customersummary()
how set inherited fields of customer object same summary object? can't cast since have down cast , run classcastexception.
i may have 10s of fields calling setters of customer object fileds of summary object result in lot of duplicate code. there smarter way handle this?
you make "copy-constructor"
protected summary(summary template){ this.name = template.name; // .... }
and call subclasses
customersummary(summary template){ super(template); }
so can do
summary summary = restclient.getstatus("1234"); customersummary customer = new customersummary(summary);
Comments
Post a Comment