Cosmic Module

J

Qubits of DPK

March 14, 2026

Core Java

Layman Explanation

When a new employee joins a company, there's an onboarding process — assign ID, set up email, assign desk. This happens ONCE when they join. Constructor is exactly that — the onboarding/setup that runs automatically when an object is created.

What is a Constructor?

A special method that automatically runs when an object is created with new. Used to initialize the object.
java
QUBITS OF DPK
1class Car {
2    String brand;
3    int speed;
4
5    Car() {                        // Constructor
6        System.out.println("Car created!");
7        brand = "Unknown";
8        speed = 0;
9    }
10}
11
12Car myCar = new Car();   // Constructor runs automatically

Constructor Rules

javascript
QUBITS OF DPK
11. Name MUST match class name exactly
22. No return type (not even void)
33. Called automatically with 'new'
44. Can be overloaded
55. If you don't write one — Java provides default constructor

Types of Constructors

1. Default Constructor

java
QUBITS OF DPK
1class Car {
2    String brand;
3    int speed;
4
5    Car() {              // default — no parameters
6        brand = "Unknown";
7        speed = 0;
8    }
9}
10Car c = new Car();       // brand=Unknown, speed=0

2. Parameterized Constructor

java
QUBITS OF DPK
1class Car {
2    String brand;
3    int speed;
4
5    Car(String brand, int speed) {  // parameterized
6        this.brand = brand;
7        this.speed = speed;
8    }
9}
10Car c = new Car("Toyota", 100);  // brand=Toyota, speed=100

3. Constructor Overloading

java
QUBITS OF DPK
1class Car {
2    String brand;
3    int speed;
4    String color;
5
6    Car() {
7        this("Unknown", 0, "White");  // calls 3-param constructor
8    }
9
10    Car(String brand, int speed) {
11        this(brand, speed, "White");  // calls 3-param constructor
12    }
13
14    Car(String brand, int speed, String color) {
15        this.brand = brand;
16        this.speed = speed;
17        this.color = color;
18    }
19}

Constructor vs Method

Java's Default Constructor

java
QUBITS OF DPK
1class Car { }   // No constructor written
2
3Car c = new Car();  // ✅ still works!
4// Java automatically provides: Car() { } (empty default constructor)
5
6// BUT if you write ANY constructor — Java STOPS providing default
7class Car {
8    Car(String brand) { this.brand = brand; }
9}
10Car c = new Car();  // ❌ compile error! Default no longer exists
11Car c = new Car("Toyota");  // ✅

️ All Traps

java
QUBITS OF DPK
1// Trap 1 — Constructor has no return type
2void Car() { }   // ❌ this is a METHOD named Car, not a constructor!
3Car() { }        // ✅ constructor
4
5// Trap 2 — Writing parameterized removes default
6class Car {
7    Car(String brand) { }
8}
9new Car();       // ❌ no default constructor anymore!
10
11// Trap 3 — Constructors not inherited
12// Child class cannot inherit parent constructor
13// Must call super() explicitly
14
15// Trap 4 — this() must be first line
16Car() {
17    System.out.println("hi");  // ❌ must be after this()
18    this("Toyota", 0);
19}

Interview Answer (SDE-2)

"A constructor is a special method with same name as class and no return type, called automatically when an object is created with 'new'. Java provides a no-arg default constructor only if no constructor is explicitly defined — writing any constructor removes it. Constructors can be overloaded (constructor overloading). this() calls another constructor and must be the first statement. Constructors are not inherited, so child classes must call super() to initialize parent. Key difference from methods: constructors initialize state, methods perform operations."

Interview Questions & MAANG-Level Answers

Q1. What is a constructor and how is it different from a method?
A constructor is a special block that runs automatically when new is called to initialize the object. Differences: (1) Constructor name must match class name exactly; methods can have any name. (2) Constructor has NO return type (not even void); methods must have return type. (3) Constructor is called automatically by JVM; methods are called explicitly. (4) Constructor cannot be inherited; methods are inherited. (5) Constructor sets up initial state; methods perform operations.
Q2. What is the default constructor?
A no-argument constructor that Java automatically provides when you don't write any constructor. It has an empty body and calls super() implicitly. Example: class Car {} — Java auto-creates Car() { super(); }. This allows new Car() to work. Default constructor has default (package-level) access unless the class is public, in which case it's public.
Q3. When does Java stop providing the default constructor?
The moment you define ANY constructor explicitly, Java stops generating the default constructor. class Car { Car(String brand) {} } — now new Car() is a compile error because only the parameterized constructor exists. Fix: explicitly add the no-arg constructor back: Car() {}. This is a very common trap when adding a new parameterized constructor to existing code that was using the default.
Q4. What is constructor overloading?
Defining multiple constructors in the same class with different parameter lists. Java picks the right one based on arguments passed:
java
QUBITS OF DPK
1Car() { this("Unknown", 0); }          // delegates to 2-param
2Car(String brand) { this(brand, 0); } // delegates to 2-param
3Car(String brand, int speed) { this.brand = brand; this.speed = speed; }
Use this() to delegate to avoid code duplication. Common in domain classes, DTOs, and entity classes.
Q5. Can a constructor be private? When would you use that?
Yes. Private constructors prevent external instantiation. Used in: (1) Singleton pattern — only one instance allowed, created internally: private static instance; private Constructor() {}. (2) Utility classes — no instances needed, only static methods: private MathUtils() { throw new UnsupportedOperationException(); }. (3) Factory method pattern — object creation controlled through static factory methods rather than direct construction.
Q6. What happens if you define a constructor with a return type?
It becomes a regular METHOD, not a constructor — even though it has the same name as the class. void Car() {} compiles successfully as a method named "Car" with void return. The JVM won't treat it as a constructor. This is a silent bug — the code compiles but your "constructor" never gets called on object creation. The real constructor (or Java's default) runs instead.