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

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -