Parameterized Constructor in Java
Overview
Parameterized constructors in Java are used to define states of the object during initialization. The parameterized constructors require one or more arguments to be passed during the initialization of an object. For "public" and "default" access modifiers, the access modifier of the constructor is the same as the access modifier of the class to whom the constructor belongs.
Follow the link to study constructors in Java.
What is Parameterized Constructor in Java
Parameterized constructors help to create objects with states defined by the programmer. Objects in java can be initialized with the default constructor or by a parameterized constructor. Initializing objects with parameterized constructors requires the same number and order of arguments to be passed by the user concerning the parameterized constructor being used. The objects created by using parameterized constructors can be unique with different data member values or states. One can have any number of parameterized constructors in a class. The parameterized constructors differ in terms of the parameters they hold. The compiler would not create a default constructor if the programmer creates their own constructor.
Why Parameterized Constructors are Used
Java class creates a default constructor if the programmer hasn't defined any type of constructor in the class. The parameterized constructor has to be defined explicitly by the programmer. The main aim to create parameterized constructor is to initialize the objects with programmer-defined states or values. The values of members can be assigned during the process of initializing the object with the help of parameterized constructors.
Examples
One can have any number of parameterized constructors in a class.
Example of Parameterized Constructor
In the below given parameterized constructor in java example, three constructors are implemented: one is the default constructor and the other two are parameterized constructors.
During initialization of an object, which constructor should get invoked depends upon the parameters one passes. For example when we create object like: namesClass n1=new namesClass("Krishna"); then the new keyword invokes parameterized constructor with string parameter (namesClass(String)) after object initialization.
Output
The above example demonstrates the use of one default and two parameterized constructors. The example shows that the type, order, and the number of arguments passed during the initialization of an object decide which constructor to invoke.
What is the return type of parameterized constructor in java?
Constructors have no return value, but the constructor returns the current instance of the class.
Error Thrown by Default Constructor
If programmer doesn't write any default constructor in the class, compiler inserts default constructor on its own. But if programmer has written any constructor in the class then compiler will not insert default constructor in that class.
Below two codes are examples demonstrating the same.
Example: 1
Output:
The above example, demonstrates the creation of default constructor by compiler when programmer hasn't written any constructor in the class.
Example: 2
Output:
The above example shows the error thrown by the use of default constructor as the compiler hasn't inserted any default constructor this time. The reason is that the user has already created one parameterized constructor in the class.
Differences Between Default Constructor and Parameterized Constructor in Java
Default Constructor | Parameterized Constructor |
---|---|
Default constructor is 0 argument constructors which contain no-argument. | Parameterized constructors have one or more than one arguments. |
Main responsibility of default constructor is to assign default values to the newly created objects. | Main aim of the parameterized constructors is to assign user-fed values to the instance variables of newly created objects. |
Default constructor is created by compiler when the programmer doesn't write any constructor in the class. | Parameterized constructor is specifically written by the programmer while creating a class. |
Conclusion
- Parameterized constructors in java are the programmer written constructors in a class which have one or more than one arguments.
- Parameterized constructors are used to create user instances of objects with user defined states.
- There can be more than one parameterized constructor in a class.
- The compiler doesn't create any default constructor in a class if programmer writes one or more custom constructors.
- Default constructor is created by compiler and has 0 arguments whereas parameterized constructor is specially written by programmer and has one or more than one arguments.