Default vs Parameterized Constructor
Constructors are used to initialize objects. In Java, the two most common types discussed in interviews are Default Constructors and Parameterized Constructors. Understanding their differences is essential for object creation, initialization control, and clean OOP design.
What Is a Default Constructor?
A default constructor is a constructor that takes no parameters.
Types of Default Constructors
There are two interpretations commonly used in interviews:
1. Compiler-Provided Default Constructor
- Created automatically by the compiler
- Only provided if no constructor is written
- Initializes instance variables with default values
class Student {
int age;
}
Compiler generates:
Student() { }
2. User-Defined No-Argument Constructor
class Student {
Student() {
System.out.println("No-arg constructor");
}
}
✔ Still called a default (no-arg) constructor in practice.
What Is a Parameterized Constructor?
A parameterized constructor accepts arguments and initializes the object with custom values at creation time.
class Student {
String name;
int age;
Student(String name, int age) {
this.name = name;
this.age = age;
}
}
Object creation:
Student s = new Student("John", 20);
Key Differences: Default vs Parameterized Constructor
| Feature | Default Constructor | Parameterized Constructor |
|---|---|---|
| Parameters | No | Yes |
| Who creates it | Compiler / Programmer | Programmer |
| Purpose | Default initialization | Custom initialization |
| Flexibility | Low | High |
| Values assigned | Default values | User-defined values |
| Interview relevance | High | Very high |
Behavior When Both Are Present
class Test {
Test() {
System.out.println("Default");
}
Test(int x) {
System.out.println("Parameterized");
}
}
- Both constructors coexist
- This is constructor overloading
Important Interview Rule (Very Important)
If you define any constructor, the compiler will NOT generate a default constructor.
class Test {
Test(int x) { }
}
❌ This will cause an error:
new Test(); // Compile-time error
✔ Solution:
Test() { }
Constructor Call Flow Comparison
Default Constructor
Student s = new Student();
- No arguments passed
- Object initialized with default values
Parameterized Constructor
Student s = new Student("Alice", 25);
- Values passed at runtime
- Object initialized meaningfully
When to Use Each
Use Default Constructor When:
- Values are optional
- Object can start with defaults
- Frameworks require no-arg constructor (e.g., serialization, reflection)
Use Parameterized Constructor When:
- Object must start in a valid state
- Mandatory fields exist
- Business rules require initialization
Common Beginner Mistakes
- Assuming default constructor always exists
- Forgetting to define no-arg constructor when needed
- Confusing default values with user-defined values
- Not using this keyword correctly
Interview-Ready Answers
Short Answer
A default constructor has no parameters and initializes objects with default values, while a parameterized constructor initializes objects with user-defined values.
Detailed Answer
In Java, a default constructor either comes from the compiler or is explicitly written without parameters. A parameterized constructor accepts arguments and allows custom initialization. If a class defines a parameterized constructor, the compiler does not generate a default constructor automatically.
Key Takeaway
Default constructors provide simplicity. Parameterized constructors provide control. A well-designed class often uses both, enabling flexible and safe object creation.