← Back to Home

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.

Default vs Parameterized Constructor Examples

1. Default Constructor (Compiler-Provided)

class Demo {
    int x;
}

class Test {
    public static void main(String[] args) {
        Demo d = new Demo();
        System.out.println(d.x);
    }
}
          

Explanation

  • No constructor written by programmer
  • Compiler creates default constructor
  • Output: 0

2. Parameterized Constructor

class Demo {
    int x;

    Demo(int x) {
        this.x = x;
    }

    public static void main(String[] args) {
        Demo d = new Demo(10);
        System.out.println(d.x);
    }
}
          

Explanation

  • Value passed during object creation
  • Output: 10

3. Default Constructor Is NOT Generated If Parameterized Exists

class Demo {
    int x;

    Demo(int x) {
        this.x = x;
    }

    public static void main(String[] args) {
        // Demo d = new Demo();  // Compile-time error
    }
}
          

Explanation

  • Once you define a constructor, compiler does not add default one

4. Explicit Default + Parameterized Constructors

class Demo {
    int x;

    Demo() {
        x = 0;
    }

    Demo(int x) {
        this.x = x;
    }

    public static void main(String[] args) {
        Demo d1 = new Demo();
        Demo d2 = new Demo(20);

        System.out.println(d1.x);
        System.out.println(d2.x);
    }
}
          

Explanation

  • Both constructors coexist
  • Output:
  • 0
  • 20

5. Using this() to Call Parameterized from Default

class Demo {
    int x;

    Demo() {
        this(10);
    }

    Demo(int x) {
        this.x = x;
    }

    public static void main(String[] args) {
        Demo d = new Demo();
        System.out.println(d.x);
    }
}
          

Explanation

  • Constructor chaining
  • Output: 10

6. Calling Default from Parameterized Constructor

class Demo {
    int x;

    Demo() {
        x = 5;
    }

    Demo(int x) {
        this();
        this.x = x;
    }

    public static void main(String[] args) {
        Demo d = new Demo(20);
        System.out.println(d.x);
    }
}
          

Explanation

  • this() must be first statement
  • Output: 20

7. Parameterized Constructor Without this

class Demo {
    int x;

    Demo(int a) {
        x = a;
    }

    public static void main(String[] args) {
        Demo d = new Demo(15);
        System.out.println(d.x);
    }
}
          

Explanation

  • Works when parameter name differs
  • Output: 15

8. Variable Shadowing Without this (Bug)

class Demo {
    int x;

    Demo(int x) {
        x = x;   // wrong
    }

    public static void main(String[] args) {
        Demo d = new Demo(30);
        System.out.println(d.x);
    }
}
          

Explanation

  • Parameter shadows instance variable
  • Output: 0

9. Fixing Shadowing Using this

class Demo {
    int x;

    Demo(int x) {
        this.x = x;
    }

    public static void main(String[] args) {
        Demo d = new Demo(30);
        System.out.println(d.x);
    }
}
          

Explanation

  • Correct assignment
  • Output: 30

10. Default Constructor in Inheritance

class A {
    A() {
        System.out.println("A");
    }
}

class B extends A {
    B() {
        System.out.println("B");
    }

    public static void main(String[] args) {
        new B();
    }
}
          

Explanation

  • Parent default constructor called first
  • Output:
  • A
  • B

11. Parameterized Constructor in Parent Class

class A {
    A(int x) {
        System.out.println(x);
    }
}

class B extends A {
    B() {
        super(10);
    }

    public static void main(String[] args) {
        new B();
    }
}
          

Explanation

  • Must explicitly call super(x)
  • Output: 10

12. Missing super() Call (Compile Error)

class A {
    A(int x) {}
}

class B extends A {
    // B() {}  // Compile-time error
}
          

Explanation

  • No default constructor in parent
  • Compiler cannot insert super()

13. Constructor Overloading with Default Values

class User {
    String role;

    User() {
        role = "Guest";
    }

    User(String role) {
        this.role = role;
    }

    public static void main(String[] args) {
        System.out.println(new User().role);
        System.out.println(new User("Admin").role);
    }
}
          

Explanation

  • Different object states
  • Output:
  • Guest
  • Admin

14. Default vs Parameterized in Object Array

class Demo {
    int x;

    Demo() {
        x = 1;
    }

    Demo(int x) {
        this.x = x;
    }

    public static void main(String[] args) {
        Demo[] arr = {
            new Demo(),
            new Demo(5)
        };

        System.out.println(arr[0].x);
        System.out.println(arr[1].x);
    }
}
          

Explanation

  • Each object uses its own constructor
  • Output:
  • 1
  • 5

15. Private Default + Public Parameterized Constructor

class Config {
    int value;

    private Config() {}

    Config(int value) {
        this.value = value;
    }

    public static void main(String[] args) {
        Config c = new Config(10);
        System.out.println(c.value);
    }
}
          

Explanation

  • Restricts object creation style
  • Output: 10

16. Constructor Execution Count

class Demo {
    Demo() {
        System.out.println("Constructor");
    }

    public static void main(String[] args) {
        new Demo();
        new Demo();
    }
}
          

Explanation

  • Constructor runs once per object
  • Output:
  • Constructor
  • Constructor

17. Default Constructor with Instance Block

class Demo {
    {
        System.out.println("Instance Block");
    }

    Demo() {
        System.out.println("Default Constructor");
    }

    public static void main(String[] args) {
        new Demo();
    }
}
          

Explanation

  • Instance block executes before constructor
  • Output:
  • Instance Block
  • Default Constructor

18. Parameterized Constructor with Instance Block

class Demo {
    {
        System.out.println("Instance Block");
    }

    Demo(int x) {
        System.out.println("Parameterized Constructor");
    }

    public static void main(String[] args) {
        new Demo(10);
    }
}
          

Explanation

  • Same execution order
  • Output:
  • Instance Block
  • Parameterized Constructor

19. Default vs Parameterized in Real-World Example

class Browser {
    String name;

    Browser() {
        name = "Chrome";
    }

    Browser(String name) {
        this.name = name;
    }

    public static void main(String[] args) {
        System.out.println(new Browser().name);
        System.out.println(new Browser("Firefox").name);
    }
}
          

Explanation

  • Default values vs user-defined values
  • Output:
  • Chrome
  • Firefox

20. Interview Summary – Default vs Parameterized Constructor

class Demo {
    int x;

    Demo() {
        x = 0;
    }

    Demo(int x) {
        this.x = x;
    }

    public static void main(String[] args) {
        System.out.println(new Demo().x);
        System.out.println(new Demo(10).x);
    }
}
          

Explanation

  • Default → initializes with defaults
  • Parameterized → initializes with passed values
  • Output:
  • 0
  • 10