The default constructor is a type of constructor that is called automatically when the programmer has not defined any constructor in the program. In contrast, the parameterized constructor is a type of constructor defined by the programmer by passing parameters to give initial values to the instance variables in the class. That is the main difference between default and parameterized constructor.
Most high-level programming languages support Object Oriented Programming (OOP). In OOP, everything is a class, which helps to build objects. These objects communicate with each other in the program. The constructor is a concept in OOP. It is a special method that allows initializing an object on its creation. The constructor can either be default or parameterized.
Key Areas Covered
1. What is Default Constructor
– Definition, Functionality
2. What is Parameterized Constructor
– Definition, Functionality
3. What is the Difference Between Default and Parameterized Constructor
– Comparison of Key Differences
Key Terms
Constructor, Default Constructor, OOP, Parameterized Constructor
What is Default Constructor
The constructor is called when an object is created. It also allocates memory for that object. Moreover, it helps to give initial values to the instance variables in the class. If the programmer does not define a constructor, the program calls the default constructor automatically. It initializes all the member variables to zero or null.
Figure 1: Program with Default Constructor
In the above program, there is a Student class. It has two instance variables as id and name. The programmer did not define any constructor. Also, there is a student object in the main method. Finally, the id and name print on the console. As the programmer did not define any constructor, the program calls the default constructor. It initializes the id to 0 and name to null.
What is Parameterized Constructor
The parameterized constructor is a constructor that accepts parameters. There can be one or more parameters. When there is a parameterized constructor, the program does not call the default constructor. Furthermore, the programmer can declare the parameters inside the parentheses after the constructor name.
Figure 2: Program with Parameterized Constructor
Above is a class called Calculation. It has two instance variables called num1 and num2. In line 7, there is a parameterized constructor. It takes two arguments x and y and assigns those values to the instance variables num1 and num2.
Furthermore, there is a method called sum. It will return the summation of those two numbers. There is an object of Calculation in the main method. The sum method is called using obj1. Finally, the results print on the console.
Difference Between Default and Parameterized Constructor
Definition
The default constructor is a constructor that the compiler automatically generates in the absence of any programmer-defined constructors. Conversely, the parameterized constructor is a constructor that the programmer creates with one or more parameters to initialize the instance variables of a class. Thus, this explains the main difference between default and parameterized constructor.
Number of Parameters
Parameters are a major difference between default and parameterized constructor. The default constructor has no parameters while parameterized constructor has one or more parameters.
Method of Calling
If the programmer skips writing a constructor, the program calls the default constructor automatically. On the other hand, the programmer should write his own constructor when writing a parameterized constructor.
Conclusion
The default constructor is a type of constructor that is called automatically when the programmer has not defined any constructor in the program. In contrast, the parameterized constructor is a type of constructor defined by the programmer by passing parameters to give initial values to the instance variables in the class. That is the main difference between default and parameterized constructor.
Reference:
1. “Java Constructor – Javatpoint.” Www.javatpoint.com, Available here.
ncG1vNJzZmiolZm2oq2NnKamZ6edrrV5yKxkraCVYrGqssWeqZ6mk5p6o7HTsJyepl2Zsqet1KWrZpmemXqxrdGapJ6slae2u7HDZpqopqOpv7av06ipaA%3D%3D