Java: Parametrized Constructor with example

In this article, we will go through parameterized constructor in detail

Parameterized Constructor :

  • A constructor which takes one or more arguments is known as parameterized constructor

 

Employee.java

package in.bench.resources.constructor.example;

public class Employee {

	// member variables
	int employeeId;
	String employeeName;

	// parametrized constructor
	Employee(int id, String name) {
		System.out.println("Employee class >> Inside parametrized constructor\n");
		this.employeeId = id;
		this.employeeName = name;

	}

	// display() method
	void display() {
		System.out.println("display() method is invoked");
	}

	// main() method - entry point to JVM
	public static void main(String args[]) {
		Employee emp = new Employee(19, "Rahul Dravid");
		emp.display();
	}
}

Output:

Employee class >> Inside parametrized constructor

display() method is invoked

Explanation :

  • Here in this example, Employee object is created using below line of code
    Employee emp = new Employee(19, “Rahul Dravid”);
  • Which invokes 2-parameter constructor and prints the message inside parameterized constructor with two arguments (int, String)
  • And then using object ‘emp’ created, invokes member method display() which prints the message inside method
  • So basically depending on number of arguments constructor takes, accordingly we need to pass those number of parameters while constructing the object
  • Otherwise will end with compilation error

Let us move on and tweak the above code to see execution results

Compilation error scenario

  • In Employee class, there is one 2-argument constructor
  • Example: Try to construct an object using below line of code which results compilation error
    Employee emp = new Employee();
  • Reason: There is no default constructor defined in class

1_Java__Parametrized_Constructor_compilation_error

 

Twist :

  • Wait, earlier we discussed that compiler inserts default no-arg constructor during compilation when we are discussing about default constructor with example

 

So, why compiler doesn’t inserts any default constructor in this case ?

  • Compiler inserts default constructor only when there is no constructor defined inside class either it is default or parameterized constructor
  • Since this example already got a 2-argument parameterized constructor defined so compiler doesn’t inserts any default constructor

What we can do for successful compilation ?

  • Have one more constructor with zero arguments that is default constructor

Is it okay to have more than one constructor inside class ?

  • Absolutely, it is fine to have more than one constructor inside class

So, we can overload constructor similar to methods ?

  • Of course, we can overload constructor depending on the business requirements similar to method overloading
  • But constructor overloading is different comparing with method overloading

Read more on constructor overloading here

Is overriding also possible ?

  • Constructor doesn’t support inheritance concept and therefore no point of overriding constructor or
  • In simple words, constructor overriding is not possible

In the following article, we will discuss in detail about constructor overloading concept

 

Read Also:

 

Happy Coding !!
Happy Learning !!

Java: Default constructor v/s Parametrized constructor
Java: Default Constructor with example