Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advantages of Constructor Overloading

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.

like image 721
S. Chatterjee Avatar asked Aug 26 '13 12:08

S. Chatterjee


2 Answers

Constructor overloading is very useful for simulate default values, or to construct an object from an already existing instance (copy)
Here is an example:

public class Color {

    public int  R, G, B, A;

    // base ctr
    public Color(int R, int G, int B, int A) {
        this.R = R;
        this.G = G;
        this.B = B;
        this.A = A;
    }

    // base, default alpha=255 (opaque) 
    public Color(int R, int G, int B) {
        this(R, G, B, 255);
    }

    // ctr from double values
    public Color(double R, double G, double B, double A) {
        this((int) (R * 255), (int) (G * 255), (int) (B * 255), (int) (A * 255));
    }

    // copy ctr
    public Color(Color c) {
        this(c.R, c.G, c.B, c.A);
    }

}

Here, the first constructor is very simple. You specify R,G,B & Alpha value for a color.

While 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.

The third ctr shows that you can instantiate your Color object with double between 0. and 1. instead of integers.

The last one takes an Color as unique argument, it copies the given object.

The 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:

    // base ctr
    public Color(int R, int G, int B, int A) {
        this.R = R;
        this.G = G;
        this.B = B;
        this.A = A;
        ++count;
    }

countis incremented from any constructor.

like image 191
johan d Avatar answered Sep 27 '22 01:09

johan d


Considering the simplest example of constructor overloading :

Class A
{
int a;

A()
{
this.a = 0;
}

A(int a)
{
this.a = a;
}

} // end class

Advantage: 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. do read this question

Isn't it easier to Overload Methods using single constructor for flexibility?

Job 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

Moreover if I am trying to use constructor overloading to use one object to initialize another, there are simpler ways to do it!

alternative is initializer , also read

like image 23
Srinath Ganesh Avatar answered Sep 24 '22 01:09

Srinath Ganesh