What is Parameterized Constructor in Java?
In Java, a parameterized constructor is a constructor that takes one or more parameters. It is used to create objects of a class with specific initial values for their properties.
Write a Java Program to Understand that the Parametrized Constructor of the Super Class can be called from Sub Class Using super().
A subclass can use a constructor defined by its superclass by using “super” and providing any necessary arguments in the format “super(arg-list).” The arg-list specifies the arguments that the superclass constructor needs to work properly.
Why parameterized constructor is used?
- Parameterized constructors in Java are used to set values to object properties during their creation.
- It help ensure that objects are created with specific initial values.
- It provide flexibility in object creation with different argument sets.
- It reduce code duplication by avoiding separate methods for initializing object properties.
- They help make code more reliable by ensuring objects are created with specific initial values.
- They help improve the maintainability of code by reducing duplication and organizing code better.
Syntax of parameterized constructor in Java:
The syntax of a parameterized constructor in Java is as follows:
class ClassName { // Instance variables declaration // ... // Parameterized constructor declaration ClassName (type parameter1, type parameter2, ...) { // Code to initialize instance variables // ... } }
In the above syntax:
1. ClassName is the name of the class for which the parameterized constructor is being defined.
2. type is the data type of the parameter being passed to the constructor.
3. parameter1, parameter2, etc. are the names of the parameters being passed to the constructor.
4. The constructor code block initializes the instance variables of the class using the values passed as parameters.
Here is the source code of the Java Program to Understand that the Parametrized Constructor of the Super Class can be called from Sub Class Using super(). The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
import java.util.Scanner; class sup1 { int z; sup1(int x, int y) { z = x + y; System.out.println("Result:"+z); } } public class Parametrized_Constructor_Demo extends sup1 { Parametrized_Constructor_Demo(int x, int y) { super(x , y); } public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("Enter value of a:"); int a = s.nextInt(); System.out.print("Enter value of b:"); int b = s.nextInt(); Parametrized_Constructor_Demo obj = new Parametrized_Constructor_Demo(a,b); } }
1. The program uses the Scanner class from java.util package to read input from the console.
2. A new class “sup1” is created with an integer variable “z” and a constructor that takes two integer parameters, x and y.
3. The constructor calculates the sum of the two parameters and assigns the result to the “z” variable.
4. The “Parametrized_Constructor_Demo” class extends the “sup1” class and has a constructor that calls the “sup1” constructor by passing two integer parameters.
5. In the main method, a new Scanner object is created to read input.
6. The program prompts the user to enter two integer values, which are stored in variables “a” and “b“.
7. A new “Parametrized_Constructor_Demo” object is created with the “a” and “b” values as arguments, which invokes the constructor of the “Parametrized_Constructor_Demo” class.
8. The constructor of the “Parametrized_Constructor_Demo” class calls the constructor of the “sup1” class, which prints the sum of the two integer values entered by the user to the console.
$ javac Parametrized_Constructor_Demo.java $ java Parametrized_Constructor_Demo Enter value of a:32 Enter value of b:23 Result:55
To practice programs on every topic in Java, please visit “Programming Examples in Java”, “Data Structures in Java” and “Algorithms in Java”.
- Apply for Java Internship
- Practice Programming MCQs
- Practice BCA MCQs
- Practice Information Technology MCQs
- Apply for Computer Science Internship