Constructor Overloading in Java - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Random Posts

Constructor Overloading in Java

Share This

A class may contain more than one constructor. If you define multiple constructors, you should be careful about the parameters.

First, we will design multiple constructors in a class. Then we will discuss what can not be done.

Employee.java
public class Employee { int _empid; String _name; String _designation; String _address; String _gender; String _dob; String _dateOfJoining; public Employee(int _empid, String _name, String _designation, String _address, String _gender, String _dob, String _dateOfJoining) { super(); this._empid = _empid; this._name = _name; this._designation = _designation; this._address = _address; this._gender = _gender; this._dob = _dob; this._dateOfJoining = _dateOfJoining; } public Employee(int _empid, String _name, String _designation, String _gender, String _dob, String _dateOfJoining) { super(); this._empid = _empid; this._name = _name; this._designation = _designation; this._gender = _gender; this._dob = _dob; this._dateOfJoining = _dateOfJoining; } }
TestMain.java
public class TestMain { public static void main(String[] args) { Employee emp1 = new Employee(1, "Ranjan Parekh", "Manager", "Male", "10/12/1992", "22/5/2014"); emp1.printValues(); Employee emp2 = new Employee(2, "John Smith", "Trainee", "1/A, XYZ Street, ABC", "Male", "10/12/1998", "13/3/2018"); emp2.printValues(); } }

So, you do understand that multiple constructors can be defined in a class.

Now, look at the following code. It will not compile. Then try to understand why this class is not compiling?

This class contains two constructors with the same number of arguments. Don't think that two constructors can not be defined by the same number of arguments. this class is not compiled because two constructors are conflicting. In the last two constructors, the number of parameters are same and the sequence of data types is also the same. Hence, if these constructors are called, the runtime environment will not understand which one is to be called.

So, if you define two constructors with the same number of parameters, be careful. Those constructors should have a different sequence of data types as parameters.



public class Employee { int _empid; String _name; String _designation; String _address; String _gender; String _dob; String _dateOfJoining; public Employee(int _empid, String _name, String _designation, String _address, String _gender, String _dob, String _dateOfJoining) { super(); this._empid = _empid; this._name = _name; this._designation = _designation; this._address = _address; this._gender = _gender; this._dob = _dob; this._dateOfJoining = _dateOfJoining; } public Employee(int _empid, String _name, String _designation, String _gender, String _dob, String _dateOfJoining) { super(); this._empid = _empid; this._name = _name; this._designation = _designation; this._gender = _gender; this._dob = _dob; this._dateOfJoining = _dateOfJoining; } public Employee(int _empid, String _name, String _designation, String _address, String _dob, String _dateOfJoining) { super(); this._empid = _empid; this._name = _name; this._designation = _designation; this._address = _address; this._dob = _dob; this._dateOfJoining = _dateOfJoining; } }

Hope, you have the basic understanding about constructors. Now we may try to write a program multiple objects of the Employee class will be created.

Content of Emloyee.java
public class Employee { int _empid; String _name; String _designation; String _address; String _gender; String _dob; String _dateOfJoining; public Employee(int _empid, String _name, String _designation, String _address, String _gender, String _dob, String _dateOfJoining) { super(); this._empid = _empid; this._name = _name; this._designation = _designation; this._address = _address; this._gender = _gender; this._dob = _dob; this._dateOfJoining = _dateOfJoining; } public Employee(int _empid, String _name, String _designation, String _gender, String _dob, String _dateOfJoining) { super(); this._empid = _empid; this._name = _name; this._designation = _designation; this._gender = _gender; this._dob = _dob; this._dateOfJoining = _dateOfJoining; } public Employee(int _empid, String _name, String _designation, String _address, String _dob, String _dateOfJoining) { super(); this._empid = _empid; this._name = _name; this._designation = _designation; this._address = _address; this._dob = _dob; this._dateOfJoining = _dateOfJoining; } }
Content of TestMain.java
public class TestMain { public static void main(String[] args) { new Employee(1, "Ranjan Parekh", "Manager", "Male", "10/12/1992", "22/5/2014"); new Employee(2, "John Smith", "Trainee", "1/A, XYZ Street, ABC", "Male", "10/12/1998", "13/3/2018"); } }


Happy Exploring!

No comments:

Post a Comment