Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would you need to use more than one constructor?

I am learning Java as of right now and have just been learned what constructors are. I do not understand why you would need more than one constructor if you need to initialize all variables.

like image 316
Tren46 Avatar asked May 30 '16 04:05

Tren46


People also ask

Can there be more than one constructor in a class?

The technique of having two (or more) constructors in a class is known as constructor overloading. A class can have multiple constructors that differ in the number and/or type of their parameters. It's not, however, possible to have two constructors with the exact same parameters.

Why constructor is needed can we have more than one constructor in a class if yes explain the need for such situation using program?

Yes, a Class in ABL can have more than Constructor. Multiple instance constructors can be defined for a class that are overloaded with different parameter signatures. If an instance constructor is defined without parameters, that constructor becomes the default instance constructor for the class.

What is the purpose of using constructors?

The purpose of a constructor is to create an object and set values if there are any object properties present. It's a neat way to create an object because you do not need to explicitly state what to return as the constructor function, by default, returns the object that gets created within it.

Can you have more than one constructor in a class C++?

In C++, We can have more than one constructor in a class with same name, as long as each has a different list of arguments.


2 Answers

To put it simply, you use multiple constructors for convenience (1st example) or to allow completely different initialization methods or different source types (2nd example.

You might need multiple constructors to implement your class to simply allow omitting some of the parameters that are already setup:

//The functionality of the class is not important, just keep in mind parameters influence it.
class AirConditioner{
   enum ConditionerMode{
      Automatic, //Default
      On,
      Off
   }
   public ConditionerMode Mode; //will be on automatic by default.
   public int MinTemperature = 18;
   public int MaxTemperature = 20;

   public AirConditioner(){ //Default constructor to use default settings or initialize manually.
      //Nothing here or set Mode to Automatic. 
   }

   //Mode
   public AirConditioner(ConditionerMode mode){ //Setup mode, but leave the rest at default
      Mode = mode;
   }
   //setup everything.
   public AirConditioner(ConditionerMode mode, int MinTemp, int MaxTemp){
      Mode = mode;
      MinTemperature = MinTemp;
      MaxTemperature = MaxTemp;
   }
}

Another example is when different constructors follow different procedures to initialize the variables. For instance you could have a data table that simply displays a table of text. The constructor could get the data from either database OR a file:

class DataTable{
   public DataTable(){} //Again default one, in case you want to initialize manually

   public DataTable(SQLConnection con, SQLCommand command){
      //Code to connect to database get the data and fill the table
   }

   public DataTable(File file){
      //Code to read data from a file and fill the table
   }
}
like image 177
Zero Avatar answered Sep 29 '22 19:09

Zero


A class can have multiple constructors, as long as their signature (the parameters they take) are not the same. You can define as many constructors as you need. When a Java class contains multiple constructors, we say that the constructor is overloaded (comes in multiple versions). This is what constructor overloading means, that a Java class contains multiple constructors.

Having said that, it is completely dependent upon your implementation whether or not you want to create more than one constructor in your class but having more than one constructor can ease your life in many instances. Suppose below class doesn't have a default constructor:

public class Employee {

    private int age;
    private String name;

    Employee(int age, String name){
        this.age=age;
        this.name=name;     
    }
}

So, while creating object of this class user would not be able to do so until he has age and name parameters handy which restricts the true functionality of Java objects as Objects' state should be able to be modified and populated at any time once initialized.

like image 24
Harish Vashist Avatar answered Sep 29 '22 19:09

Harish Vashist