In this post, we will learn the difference between Default constructor and Parameterized constructor in Java. This is a frequently asked question in Java interviews for beginners. Let's dive into it.
Feature | Default Constructor | Parameterized Constructor |
---|---|---|
Definition | A constructor with no parameters is automatically created by the compiler if no constructor is explicitly defined in the class. | A constructor with one or more parameters, which is explicitly defined in the class, is called a parameterized constructor. |
Parameters | No parameters are passed to the default constructor. | One or more parameters are passed to the parameterized constructor. |
Initialization | The default constructor usually initializes instance variables to their default values (e.g., 0 for numeric types, null for object references). | The parameterized constructor initializes instance variables with the values passed as parameters. |
Usage | Used when an object is created without providing specific initial values for its instance variables. | Used when an object is created with specific initial values for its instance variables. |
Customization | Since it is automatically generated by the compiler, it may not provide any customization options. | Provides customization options by allowing developers to pass specific values to initialize the object's state. |
Overloading | Multiple classes can share the same default constructor if they have no explicit constructors defined. | Multiple parameterized constructors can coexist in the same class, allowing different ways to create objects. |
Calling Superclass Constructor | If the superclass has a parameterized constructor only, then the default constructor in the subclass won't be automatically created, and you need to explicitly call the superclass's parameterized constructor using the super() keyword. | If the superclass has a parameterized constructor, the subclass's parameterized constructor can use the super() keyword to call the superclass's parameterized constructor. The default constructor in the subclass is not automatically created. |
Default Constructor:
A default constructor is a constructor that takes no arguments. If a class does not define any constructor, the compiler automatically provides a default constructor with no parameters.
class Person {
String name;
int age;
// Default Constructor
public Person() {
name = "Unknown";
age = 0;
}
void displayInfo() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
public class DefaultVsParameterizedConstructor {
public static void main(String[] args) {
// Creating an object using the default constructor
Person person1 = new Person();
person1.displayInfo();
}
}
Output:
Name: Unknown, Age: 0
In the example above, the Person class defines a default constructor that sets the name to "Unknown" and age to 0. When we create an object using the default constructor, the default values are assigned to the instance variables.
Parameterized Constructor:
A parameterized constructor is a constructor that takes one or more parameters. It allows us to initialize the instance variables with specific values at the time of object creation.
class Person {
String name;
int age;
// Parameterized Constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
void displayInfo() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
public class DefaultVsParameterizedConstructor {
public static void main(String[] args) {
// Creating an object using the parameterized constructor
Person person2 = new Person("John", 30);
person2.displayInfo();
}
}
Output:
Name: John, Age: 30
In the example above, the Person class defines a parameterized constructor that takes name and age as parameters and initializes the instance variables with the provided values. When we create an object using the parameterized constructor, we can pass specific values for the name and age attributes.
Interview Questions
Java
X vs Y
Comments
Post a Comment