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
Constructor Rules
javascript
QUBITS OF DPK
Types of Constructors
1. Default Constructor
java
QUBITS OF DPK
2. Parameterized Constructor
java
QUBITS OF DPK
3. Constructor Overloading
java
QUBITS OF DPK
Constructor vs Method
Java's Default Constructor
java
QUBITS OF DPK
️ All Traps
java
QUBITS OF DPK
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
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.