What is a Constructor in OOP?
Learn what a constructor is in OOP — default constructors, overloading and chaining with this/super — with Java examples and interview questions.
Expected Interview Answer
A constructor is a special method invoked automatically when an object is created that initializes the object’s state, sharing the class’s name and having no return type.
A class can define multiple constructors with different parameter lists (constructor overloading), and if no constructor is written, the compiler supplies a no-argument default constructor. Constructors can chain to one another (this(...)) or to a parent class constructor (super(...)), ensuring initialization happens in a controlled, top-down order through the inheritance chain. Unlike ordinary methods, constructors are not inherited and cannot be called directly after object creation — they only run once, at instantiation.
- Guarantees an object starts in a valid, fully initialized state
- Supports multiple ways to create an object via overloading
- Enables constructor chaining for shared initialization logic
- Runs automatically, so initialization can’t be forgotten
AI Mentor Explanation
Before a player takes the field, there’s a mandatory kit-and-fitness check — pads on, helmet fitted, fitness cleared — that runs exactly once and sets them up ready to play. You don’t get onto the pitch without it, and it always happens the same way, in the same order, before anything else. A constructor is that setup step for an object: it runs automatically once at creation, ensuring the object starts in a valid, ready state before any other method can be called on it.
Step-by-Step Explanation
Step 1
Same name as the class
A constructor’s name must exactly match its class name, with no return type.
Step 2
Runs automatically on instantiation
Invoked implicitly whenever new ClassName(...) is called.
Step 3
Initializes fields
Sets instance variables to valid starting values, often from parameters.
Step 4
Can be overloaded or chained
Multiple constructors can exist; this(...) or super(...) chain to other constructors.
What Interviewer Expects
- Correct definition: no return type, same name as class, runs at instantiation
- Understanding of the default (no-arg) constructor
- Awareness of constructor overloading and chaining (this/super)
- Knowing constructors are not inherited by subclasses
Common Mistakes
- Saying a constructor can have a return type (even void)
- Believing constructors are inherited like normal methods
- Forgetting that defining any constructor removes the compiler’s default one
- Not knowing super() is called implicitly if omitted
Best Answer (HR Friendly)
“A constructor is a special block of code that runs automatically when an object is created, setting up its initial state. It has the same name as the class, no return type, and can be overloaded to allow creating objects in different ways.”
Code Example
class Point {
int x, y;
Point() {
this(0, 0); // chain to the two-arg constructor
}
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
Point origin = new Point(); // uses default (0, 0)
Point p = new Point(3, 4); // uses explicit coordinatesFollow-up Questions
- What is the difference between a constructor and a regular method?
- What happens if you don’t define any constructor in a class?
- What is constructor chaining and when is it useful?
- Can a constructor be private, and why would you do that?
MCQ Practice
1. A constructor’s return type is?
Constructors have no return type, not even void — this is what distinguishes them from methods.
2. If a class defines no constructor, the compiler provides?
The compiler automatically supplies a no-arg default constructor when none is written.
3. Which keyword calls another constructor in the same class?
this(...) invokes another constructor of the same class for constructor chaining.
Flash Cards
Constructor in one line? — A special method, same name as the class, no return type, run automatically at object creation.
Default constructor? — A no-argument constructor the compiler provides if none is defined.
Constructor chaining? — Using this(...) or super(...) to call another constructor from within one.
Are constructors inherited? — No — subclasses must define their own, though they can call super(...).