I want to create an instance of a subclass that contains all the same fields as the parent Object, just with an extra field or two. My plan is this:
public subclass(parentClass parent, String extraField)
{
super(parent.getField1(), parent.getField2());
this.extraField = extraField;
}
Is there anything wrong with this design?
That's fine, it's just a copy-constructor-plus.
But ideally, define a copy-constructor on the parent class and use it instead:
public Subclass(ParentClass parent, String extraField) {
super(parent); // <=== Using the copy constructor
this.extraField = extraField;
}
(Also, ideally, use Java standard naming conventions, at least when posting code for others to read... ;-) )
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.