Java is one of the most popular object-oriented programming languages that allows programmers to create objects and change their behavior using functions and methods. We use constructors to initialize the objects with the values. The constructor is called by default when the object of a class is created. There are mainly two types of constructors present in java which are default constructors in java and parameterized constructors in java. While moving further in this article we will learn all about parameterized constructors in java, with their syntax, examples, and applications.
What is a Parameterized Constructor in Java?
As the name suggests parametrized constructor in java is a type of constructor that takes one or more parameters as arguments. It is used to initialize the object with the values passed by the user as the arguments while declaring the object.
In simple words, the parametrized constructor in java helps the user to initialize the object at the time of its creation.
Syntax of Parameterized Constructor in Java
The syntax of parameterized constructor in java is given below:
class ClassName {
TypeName variable1;
TypeName variable2;
ClassName(TypeName variable1, TypeName variable2) {
this.variable1 = variable1;
this.variable2 = variable2;
}
}
In the above syntax, the ClassName is the name of the class. TypeName refers to the datatypes of the variables.
After this, we created a parametrized constructor in java, which takes two arguments and will assign the value of the variables with the values given in the arguments.
As with any normal constructor, the name of parametrized constructor in java is the same as that of the name of the class.
Examples of Parameterized Constructor in Java
In this section, we will discuss various examples of parametrized constructor in java.
Example 1 of Parameterized Constructor in Java: Initializing the object
In the following example we will create some variables in the class and then try to initialize their value by using the parametrized constructor.
Code Implementation
class Student { String name; int rollNumber; double marks; Student(String name, int rollNumber, double marks) { this.name = name; this.rollNumber = rollNumber; this.marks = marks; } public void displayDetails() { System.out.println("Name: " + name); System.out.println("Roll Number: " + rollNumber); System.out.println("Marks: " + marks); } } public class Main { public static void main(String[] args) { Student student = new Student("John", 101, 95.5); student.displayDetails(); } }
Output
Name: John
Roll Number: 101
Marks: 95.5
Explanation of the above example
In the above example, we have created a class with the name student containing three instance variables in it and they are name, rollNumber, and marks. These three variables are string, int, and double data types respectively. We have created a parametrized constructor that will take the values from the user and will initialize the object’s variable with those values at the time of object creation with the help of arguments in the parameters. In the output, we can see that the passed parameters are displayed.
Example 2 of Parameterized Constructor in Java: With Overloading
There is no limitation on the number of constructors in java we can have multiple constructors in the same class with different parameters. Now let’s discuss this concept with the help of an example.
Code Implementation
class Rectangle { int length; int width; Rectangle(int length, int width) { this.length = length; this.width = width; } Rectangle(int length) { this.length = length; this.width = 0; } public int calculateArea() { return length * width; } } public class Main { public static void main(String[] args) { Rectangle rectangle1 = new Rectangle(10, 20); System.out.println("Area of rectangle1: " + rectangle1.calculateArea()); Rectangle rectangle2 = new Rectangle(15); System.out.println("Area of rectangle2: " + rectangle2.calculateArea()); } }
Output
Area of rectangle1: 200
Area of rectangle2: 0
Explanation of the above example
In the above example, we have created a class rectangle with two variables length and width. In this class, we have created two constructors one will take the input parameters from the user and initialize both length and width with the user-given values whereas the second one will assign the length with the user-given value but in the case of width, we assign it 0. So as you can see in the above output the first rectangle prints the area correctly and the second rectangle prints the output as 0.
Advantages of Parameterized Constructor in Java
Parameterized constructor in java has the following advantages:
- Parametrized constructor in java helps in the initialization of instance variables. The object will be initialized with the values passed as a reference by the user.
- The parameterized constructor in java provides flexibility to the user or programmer to create various objects with different values.
- They also improve the readability as what we can read the values passed in the object.
- They are easy to maintain as we can easily make any change to the instance variable.
Conclusion
In java, we use constructors to create the objects of a class. Parameterized constructor in java takes one or more than one parameter and initializes the object with the values given by the user during object creation. Parameterized constructor in java provides flexibility to the programmer to create objects with different values of instance variables, it makes the code more readable and is also helpful in encapsulating the data. We have seen the use of parameterized constructors in java with the help of examples, at last, we have also discussed the application of parametrized constructor in java.
Frequently Asked Questions
Here are some of the frequently asked questions about parametrized constructor in java.
1. Can we have multiple parameterized constructors in Java?
Yes, we can have multiple parameterized constructors in Java, and this is called constructor overloading.
2. How is a parameterized constructor different from a default constructor?
A parameterized constructor takes one or more parameters as input, whereas a default constructor takes no parameters. A default constructor initializes the instance variables with default values, whereas a parameterized constructor initializes the instance variables with the values passed as arguments.
3. How do you call a parameterized constructor in Java?
To call a parameterized constructor in Java, you need to create an object of the class and pass the required parameters as arguments to the constructor.
4. What happens if we don’t define a constructor in a class in Java?
If we don’t define a constructor in a class in Java, a default constructor is automatically generated by the Java compiler.
5. Can we have a constructor and a method with the same name in Java?
No, we cannot have a constructor and a method with the same name in Java.