Constructor Program in Java

Problem Description

Create a Java program to demonstrate the use of Constructor.

What is Constructor in Java?

A constructor in Java is a special type of method that is used to initialize an object. It is called when an instance of an object is created and can be used to set the initial values for the object’s properties. Constructors are similar to regular methods, but they have a few key differences:

  • constructor does not have a return type.
  • They must have the same name as the class they are defined in
  • They can’t be inherited from subclasses.
  • They’re always called when an instance of an object is created Constructors are useful for setting up objects so that they’re ready to be used immediately after they’re created.

Types of Constructors in Java
There are two types of constructors in Java

  • Default Constructor
  • Parameterized Constructor

Default Constructor: The default constructor is invoked when you create an object without passing any arguments to the constructor. It simply creates an instance of the class with the default values for all its fields.

Parameterized Constructor: The parameterized constructor is invoked when you pass one or more arguments to the constructor. It allows you to set the initial state of the object by passing in values for its fields.

advertisement
advertisement

Here we have made two types of constructor. One is simple and other is parametric. Now depending upon the object created, constructor gets started and hence we get the output.

Program/Source Code

Here is the source code of the Java Program to Illustrate Use of Constructor. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

/*
 * Java program to demonstrate the use of Constructor
 */
 
public class Constructor
{
    double width;
    double height;
    double depth;
    Constructor() 
    {
	System.out.println("Constructor without parameter");
	width = 10;
	height = 10;
	depth = 10;
    }
    Constructor(int a, int b, int c) 
    {
	System.out.println("Constructor with parameter");
	width = a;
	height = b;
	depth = c;
    }
    double volume() 
    {
	return width * height * depth;
    }
}
class ConstructorDemo
{
    public static void main(String args[]) 
    {
	Constructor cuboid1 = new Constructor();
        double vol;
	vol = cuboid1.volume();
	System.out.println("Volume is " + vol);
	Constructor cuboid2 = new Constructor(8,11,9);
	vol = cuboid2.volume();
	System.out.println("Volume is " + vol);
    }
}
Program Explanation

1. The program defines a class named “Constructor” with three instance variables of type double: “width“, “height“, and “depth“.
2. The class has two constructors, one without any parameters and another one with three integer parameters.
3. The first constructor initializes the instance variables with default values of 10.0 and prints a message “Constructor without parameter”.
4. The second constructor sets the variables to the values passed as parameters and prints “Constructor with parameter”.
5. The class also has a method named “volume” that calculates and returns the volume of the cuboid.
6. The program creates a “ConstructorDemo” class that uses the “Constructor” class to create two objects: one using the default constructor and another using the parameterized constructor.
7. It calculates the volume of these objects using the “volume()” method and prints the results.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
Program Output:
$ javac ConstructorDemo.java
$ java ConstructorDemo
 
Constructor without parameter
Volume is 1000.0
Constructor with parameter
Volume is 792.0

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.