java - Why can we not combine all the constructors, why must there be three overloaded constructors in typical inheritance cases? -
public class circlegeometricobject extends geometricobject { private double radius; public circlegeometricobject() {} public circlegeometricobject(double radius) { this. radius = radius; } public circlegeometricobject(double radius, string color, boolean filled) { this. radius = radius; setcolor(color); setfilled( filled); } }
if wanted reduce code duplication below (add null checking though). it's possibly on top example, constructors lots of code can useful reduce duplication.
public class myclass { private double radius; public myclass() { this(null, null, null); } public class myclass(double radius) { this(radius, null, null) } public class myclass(double radius, string colour, boolean filled) { this.radius = radius; setcolour(colour); setfilled(filled); } }
** edited change double double allow null
Comments
Post a Comment