Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Differences Between Default Constructor and Parameterized Constructor in Java



The default and parameterized constructors are two types of Constructor in Java. The constructor is a special member of a Java class whose name is the same as the class name. It is used to assign values to a class variable at the time of object creation.

In this article, we are going to discuss the difference between default and parameterized constructors.

Default Constructor

When we do not add a constructor to a Java class. The compiler adds a default constructor implicitly. It accepts 0 arguments. If we do not initialize the instance variables of a class, a default constructor will initialize them with default values (i.e., 0 for integers, 0.0 for floating point values, etc.).

However, it should be noted that every default constructor has 0 arguments, but if we create a 0-argument constructor explicitly, we cannot term it as a default constructor (it is just a 0-argument constructor). The access modifier of the default constructor is always the same as a class modifier, but this rule is applicable only for public and default modifiers.

Example

An example of a default constructor is given below:

public class Student {
   void display() {
      int roll_no = 121;  
      String stu_name = "Suryavanshi"; 
      System.out.println(roll_no + " " + stu_name);
   }

   public static void main(String args[]) {
      Student s1 = new Student();
      s1.display();
   }
}

In the above Java program, there is no constructor defined. Therefore, the compiler adds a default constructor to the code. The output of the above code will be:

121 Suryavanshi

Parameterized Constructors

The parameterized constructor accepts some arguments and is explicitly defined by the programmer. Like the default constructor, its access modifier is always the same as the class modifier in the case of public and default modifiers.

Example

A Java program illustrating a parameterized constructor is given below:

public class Student {
   int roll_no;
   String stu_name;
   // Parameterized constructor
   Student(int i, String n) { 
      roll_no = i;
      stu_name = n;
   }
   void display() {
      System.out.println(roll_no+" "+stu_name);
   }
   public static void main(String args[]) {
      Student s1 = new Student(1,"Adithya");
      Student s2 = new Student(2,"Jai");
      s1.display();
      s2.display();
   }
}

In the above program, the programmer defines one parameterized constructor with 2 parameters. Now the compiler adds no default constructor to the code. Its output is as shown below:

1 Adithya
2 Jai

Default Constructor VS Parameterized Constructor

The following table discusses the difference between the Default and Parameterized Constructors of Java:

Default Constructor

Parameterized Constructor

A default constructor is a 0-argument constructor that contains a no-argument call to the super class constructor.

The parameterized constructors are the constructors that have a specific number of arguments to be passed.

Assigning default values to the newly created objects is the main responsibility of the default constructor.

The purpose of a parameterized constructor is to assign user-wanted specific values to the instance variables of different objects.

The compiler writes a default constructor in the code only if the program does not write any constructors in the class.

A parameterized constructor is written explicitly by a programmer.

Updated on: 2025-05-21T16:55:43+05:30

14K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements