java - Create Dervice Instance by passing base instance -


hi basic level question. need know best way handle this.

parent class.

public class parent {      private string familyname;      public parent() {     }      public parent(string familyname) {         this.familyname = familyname;     }      public string getfamilyname() {         return familyname;     }      public void setfamilyname(string familyname) {         this.familyname = familyname;     } 

child class.

   public class child extends parent {      string name;      public child(parent parent) {        super(parent.getfamilyname());     }      public string getname() {         return name;     }      public void setname(string name) {         this.name = name;     }      @override     public string tostring() {         return "child{" +                 "name='" + name + '\'' +                 "familyname='" + getfamilyname() + '\'' +                 '}';     } } 

or there support library create child instance cloning parent instance.

this way can do. ok when have few attributes. if have class 100+ attributes cant this. thing ugly. there way create child instance passing parent instance rather setting attribute values 1 one.

it seems me little bit confusing, have pass parent class parameter of child , have class, need input arguments in constructor. furthermore, have try passing field values, hide class inhritance, not parent class. like:

public class child extends parent {      string name;      public child(string name, string familyname) {        super(familyname);        this.name = name;     }     ... } 

or need make child having parent field, like:

public class child  {      string name;     parent parent;      public child(string name, parent parent) {        this.parent = parent;        this.name = name;     }     ... } 

you can have common interface parent , child , use purposes. or more, can left extending parent class, have instance , override methods need, like:

public class child extends parent {      string name;     parent parent;      public child(string name, parent parent) {        super();        this.parent = parent;        this.name = name;     }      @override     public string getfamilyname() {        return parent.familyname;     }      @override     public void setfamilyname(string familyname) {         parent.familyname = familyname;     }     ... } 

but, imo, need have valuable reasons use such solution, because it's not straightforward usual one.

one more solution make additional parent constructor like:

public class parent {      private string familyname;      public parent() {     }      public parent(parent clone) {         this.familyname = clone.familyname;     }      ... } 

and call as:

public class child extends parent {      string name;      public child(string name, parent parent) {        super(parent);        this.name = name;     }     ... } 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -