Home:ALL Converter>Advantages of Constructor Overloading

Advantages of Constructor Overloading

Ask Time:2013-08-26T20:34:28         Author:S. Chatterjee

Json Formatter

I am very new to Java and trying to learn the subject, having previous programming exposure in only HTML/CSS. I have started with Herbert Schildt and progressed through a few pages.

I am unable to understand the exact advantages of Constructor Overloading. Isn't it easier to Overload Methods using single constructor for flexibility? Moreover if I am trying to use constructor overloading to use one object to initialize another, there are simpler ways to do it! So what are the benefits and in which situation should I use Constructor Overloading.

Author:S. Chatterjee,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/18444198/advantages-of-constructor-overloading
proskor :

When you are dealing with immutable classes and you want to provide multiple ways to instantiate them, then overloading the constructor is convenient.",
2013-08-26T12:42:21
johan d :

Constructor overloading is very useful for simulate default values, or to construct an object from an already existing instance (copy)\nHere is an example: \n\npublic class Color {\n\n public int R, G, B, A;\n\n // base ctr\n public Color(int R, int G, int B, int A) {\n this.R = R;\n this.G = G;\n this.B = B;\n this.A = A;\n }\n\n // base, default alpha=255 (opaque) \n public Color(int R, int G, int B) {\n this(R, G, B, 255);\n }\n\n // ctr from double values\n public Color(double R, double G, double B, double A) {\n this((int) (R * 255), (int) (G * 255), (int) (B * 255), (int) (A * 255));\n }\n\n // copy ctr\n public Color(Color c) {\n this(c.R, c.G, c.B, c.A);\n }\n\n}\n\n\nHere, the first constructor is very simple. You specify R,G,B & Alpha value for a color. \n\nWhile this is enough to use the Color class, you provide a second constructor, liter, which auto assign 255 to alpha if not specified by the user.\n\nThe third ctr shows that you can instantiate your Color object with double between 0. and 1. instead of integers.\n\nThe last one takes an Color as unique argument, it copies the given object.\n\nThe benefits lies also in the fact that the first constructor is always called, and you can use that to manually count your instances. Let say you have a private static int count=0 attribute, you can track the number of Color instance like this:\n\n // base ctr\n public Color(int R, int G, int B, int A) {\n this.R = R;\n this.G = G;\n this.B = B;\n this.A = A;\n ++count;\n }\n\n\ncountis incremented from any constructor.",
2013-08-26T12:42:03
Srinath Ganesh :

Considering the simplest example of constructor overloading :\n\nClass A\n{\nint a;\n\nA()\n{\nthis.a = 0;\n}\n\nA(int a)\n{\nthis.a = a;\n}\n\n} // end class\n\n\nAdvantage: Now I may simply need to use the default constructor new A() to assign default values or for a more dynamic case specify what value it must be new A(10) which is a parametrised constructor.\ndo read this question\n\n\n Isn't it easier to Overload Methods using single constructor for flexibility?\n\n\nJob of constructor is to instantiate the object , and the job of the method is to process the object values. Thus limiting methods to processing (exception to setter methods) and constructor to creation of object will help in a long run and also make your class more usable to your team-mates\n\n\n Moreover if I am trying to use constructor overloading to use one\n object to initialize another, there are simpler ways to do it!\n\n\nalternative is initializer , also read",
2013-08-26T18:33:37
Suresh Atta :

It depends completely on how you are constructing your object.\n\n\n One of Classical example of Constructor overloading is ArrayList in Java. ArrayList has three constructors one is empty, other takes a collection object and one take initial Capacity. these overloaded constructor allows flexibility while create arraylist object. It may be possible that you don't know size of arraylist during creation than you can simply use default no argument constructor but if you know size then its best to use overloaded Constructor which takes capacity. Since ArrayList can also be created from another Collection, may be from another List than having another overloaded constructor makes lot of sense. By using overloaded constructor you can converty your ArrayList into Set or any other collection.\n\n\nMore info with general examples.",
2013-08-26T12:51:15
Jonathan Drapeau :

The way the constructor is defined will make sure that the parameters of the constructor will be passed to the object upon instantiation.\n\nOverloading a method won't \"force\" anyone to call it unlike having parameters in the constructor, where you can't instanciate an object without passing those paremeters.\n\nHaving multiple way to construct an object might be useful, like with the Color class, in ndj's answer, can be created in four different ways. Each way makes sure that Color as the minimum informations to be useful, or \"work\".\n\nThose informations could be missing if Color only had a contructor without parameter and, for the class to \"work\", would be to call setRand the other methods... which don't exist in the Color class .",
2013-08-26T12:55:21
yy