Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Parametrized Constructor in Java

Last Updated on January 18, 2024 by Ankit Kochar

Java, renowned for its object-oriented paradigm, facilitates the creation and management of objects to build robust applications. Understanding the concept of parameterized constructors in Java is vital for tailoring objects with specific initial values during instantiation. Unlike default constructors, parameterized constructors allow developers to initialize object properties with values passed as parameters during object creation. This guide delves into the intricacies of parameterized constructors, exploring their significance and providing practical insights into leveraging them effectively in Java programming.

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 conclusion, mastering the usage of parameterized constructors in Java is a crucial skill for developers striving to create more flexible and customizable objects. By allowing the specification of initial values during object instantiation, parameterized constructors enhance the adaptability and efficiency of Java code. This guide has walked through the process of defining and utilizing parameterized constructors, emphasizing their role in initializing object properties based on specific requirements. Armed with this knowledge, developers can create more dynamic and responsive Java applications that cater to diverse use cases.

Frequently Asked Questions related to Parameterized Constructor in Java

Here are some of the frequently asked questions about parametrized constructor in java.

1. What is a parameterized constructor in Java?
A parameterized constructor in Java is a special type of constructor that accepts parameters during object creation. It allows developers to initialize object properties with values passed as arguments, providing a way to create more customized and adaptable objects.

2. How do you define and use a parameterized constructor in Java?
To define a parameterized constructor in Java, follow these steps:

  • Declare the constructor with parameters in the class.
  • Inside the constructor, use the provided parameters to initialize object properties.
  • To use a parameterized constructor, pass values as arguments when creating an object, ensuring proper initialization based on specific requirements.

3. Can a class have both default and parameterized constructors in Java?
Yes, a class in Java can have both default and parameterized constructors. The default constructor is provided by the compiler if no constructor is explicitly defined, while a parameterized constructor can be added to allow more customized object initialization.

4. What are the advantages of using parameterized constructors?
Parameterized constructors offer several advantages, including:

  • Customized initialization: Objects can be initialized with specific values during creation.
  • Flexibility: Developers can create objects with different initial states based on the provided parameters.
  • Code reusability: Parameterized constructors allow for the reuse of the same class with different initializations, promoting modular and maintainable code.

5. How do parameterized constructors contribute to code efficiency in Java?
Parameterized constructors contribute to code efficiency by enabling developers to create objects with predefined states. This eliminates the need for additional setter methods after object creation, streamlining the initialization process and making the code more concise and expressive.

Leave a Reply

Your email address will not be published. Required fields are marked *