What is a Constructor in Java?
Learn what a Java constructor is, how default and overloaded constructors work, and how this() and super() chain initialization across classes.
Expected Interview Answer
A constructor is a special block of code, matching the class name and with no return type, that runs automatically when an object is created with new, and its job is to initialize the object's state.
If you write no constructor, the compiler inserts a no-argument default constructor that sets fields to their default values. Constructors can be overloaded, meaning a class may have several constructors with different parameter lists, and one constructor can call another in the same class using this(...) to avoid duplicated initialization logic. A subclass constructor implicitly or explicitly calls a superclass constructor via super(...) as its first statement, ensuring the parent's state is initialized before the child's. Unlike regular methods, constructors cannot be inherited, cannot be abstract, static, or final, and cannot be called directly like a normal method.
- Guarantees an object starts in a valid, fully-initialized state
- Overloading supports flexible ways to construct the same type
- this(...) chaining avoids duplicated initialization logic
- super(...) ensures parent state is set up before child state
- Prevents objects existing in a half-constructed, invalid form
AI Mentor Explanation
A constructor is like the pre-match toss and kit check that must happen before any ball is bowled, setting up the team's state — playing eleven, batting order — before the game truly starts. Overloaded constructors are like having both a full pre-season trial and a quick net session to select the eleven depending on how much prep time exists.
Step-by-Step Explanation
Step 1
Match name and no return type
A constructor's name must exactly match the class name and it declares no return type, not even void.
Step 2
Runs on object creation
The JVM invokes the matching constructor automatically whenever `new ClassName(...)` is executed.
Step 3
Default constructor if none written
If you declare no constructor, Java supplies a no-arg one that sets fields to their default values.
Step 4
Overload for flexibility
Define multiple constructors with different parameter lists so callers can construct the object in different ways.
Step 5
Chain with this() and super()
Use this(...) to call another constructor in the same class, and super(...) to initialize the parent class first.
What Interviewer Expects
- Knows a constructor's name matches the class and has no return type
- Understands the compiler-generated default constructor rule
- Can explain constructor overloading with an example
- Knows super() must be the first statement (implicit or explicit)
- Knows constructors are not inherited and cannot be static/final/abstract
Common Mistakes
- Giving the constructor a return type like void, making it a regular method by accident
- Assuming a subclass automatically has access to the parent's constructors as inherited methods
- Forgetting that once you write any constructor, the default no-arg one disappears
- Believing this() and super() can both appear in the same constructor body
- Not knowing that field initializers run before the constructor body executes
Best Answer (HR Friendly)
“A constructor is the special piece of code that runs automatically whenever a new object is created, and its job is to set up that object's initial data so it starts life in a valid state. A class can have multiple constructors to allow creating objects in different ways, such as with default values or with values the caller supplies.”
Code Example
class Person {
String name;
int age;
Person(String name) {
this(name, 0); // constructor chaining
}
Person(String name, int age) {
this.name = name;
this.age = age;
}
}
class Employee extends Person {
String company;
Employee(String name, int age, String company) {
super(name, age); // must be first statement
this.company = company;
}
}
public class Main {
public static void main(String[] args) {
Employee e = new Employee("Ada", 30, "SkillVeris");
System.out.println(e.name + " " + e.age + " " + e.company);
// Ada 30 SkillVeris
}
}Follow-up Questions
- What happens if you don't call super() explicitly in a subclass constructor?
- Can a constructor be private, and why would you use one?
- What is the difference between a constructor and an instance initializer block?
- Can you call a constructor like a normal method?
- What order do field initializers and constructor bodies run in?
MCQ Practice
1. What must a constructor's name match?
A constructor is always named exactly the same as its enclosing class.
2. What happens if a class defines no constructor at all?
The compiler automatically provides a no-argument default constructor when none is declared.
3. Where must a call to super(...) appear in a constructor?
super(...) must be the first statement so the parent class is fully initialized before subclass logic runs.
Flash Cards
What is a constructor's return type? — None at all — not even void.
When does Java provide a default constructor? — Only when the class declares no constructors of its own.
What does this(...) do inside a constructor? — Calls another constructor in the same class to reuse initialization logic.
Are constructors inherited by subclasses? — No — subclasses must define their own, though they can call the parent's via super(...).