Parameterized Constructor Program in Java

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.

Problem Description

Write a Java Program to Understand that the Parametrized Constructor of the Super Class can be called from Sub Class Using super().

Problem Solution

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:

advertisement
advertisement

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.

Program/Source Code

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);
    }
}
Program Explanation

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.

advertisement
Program Output:
$ 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”.

advertisement

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.