Yes, Java automatically provides a default constructor—but only under one condition:
🧾 When Does Java Provide a Default Constructor?
Java will insert a default (no-argument) constructor only if:
- You do not define any constructors yourself in the class.
📌 What Is a Default Constructor?
It’s a constructor that:
- Takes no parameters
- Has no code except calling the superclass constructor (
super()
)
📦 Example: Provided by Java
public class Person {
// No constructor defined
// Java will generate this behind the scenes:
// public Person() { super(); }
}
✅ You can now create objects like:
Person p = new Person(); // Works fine
⚠️ Important: If You Define Any Constructor…
If you define even one constructor (with or without parameters), Java will not add a default constructor automatically.
❌ Example:
public class Person {
public Person(String name) {
// Custom constructor
}
}
Person p = new Person(); // ❌ Error: no default constructor
You’d have to manually add it like:
public Person() {
// optional default behavior
}
🧠 Interview Tips
Question | Key Point |
---|---|
What is a default constructor? | A no-arg constructor added by Java |
When is it provided? | Only if no constructor is manually defined |
Can we override it? | Yes—you can write your own no-arg constructor |
What if I only write a param constructor? | Java won’t provide the default—must define it yourself |