constructors:
- ✅ Basic explanation
- 🤔 Why we use constructors
- 📌 Types of constructors
- 💭 Real-life examples
- 🧠 Most asked constructor-based interview questions (with answers)
- 🧪 Puzzles and tricky constructor questions for practice
🚀 Java Constructors – Masterclass with Examples
✅ What is a Constructor?
A constructor is a special method in a class:
- It has the same name as the class
- It has no return type (not even
void
) - It runs automatically when an object is created
🤔 Why Do We Use Constructors?
- To initialize objects
- To assign default or custom values
- To run specific logic when an object is created
📌 Types of Constructors in Java
Type | Description |
---|---|
Default Constructor | No arguments, provided by Java if none written |
No-Arg Constructor | User-defined with no parameters |
Parameterized Constructor | Takes arguments to set object values |
Copy Constructor | Creates object by copying another object's values |
💡 Example:
public class Student {
String name;
int age;
// Constructor
Student(String n, int a) {
name = n;
age = a;
}
void display() {
System.out.println(name + " - " + age);
}
public static void main(String[] args) {
Student s1 = new Student("Arun", 21);
s1.display();
}
}
🧠 Top Java Constructor Interview Questions with Answers
Q1: Can a constructor be overloaded?
✅ Yes! Just like methods, you can create multiple constructors with different parameter lists.
public class Person {
Person() { System.out.println("No-arg"); }
Person(String name) { System.out.println("Name: " + name); }
}
Q2: What happens if you don't write a constructor?
✅ Java provides a default constructor (no-args) automatically.
But if you write any constructor, Java won’t generate the default one.
Q3: Can constructors be private
?
✅ Yes. Used in Singleton Design Pattern to restrict object creation.
private ConstructorExample() {
// Cannot create from outside
}
Q4: Is it possible to call a constructor from another constructor?
✅ Yes, using this()
keyword.
Student() {
this("Default Name");
}
Student(String name) {
System.out.println(name);
}
Q5: Can constructors be inherited?
❌ No. Constructors are not inherited, but the subclass can call the superclass constructor using super()
.
🔍 Tricky Constructor Puzzle Questions
🔸 Puzzle 1:
class Test {
Test() {
System.out.println("Constructor");
}
void Test() {
System.out.println("This is a method");
}
public static void main(String[] args) {
Test t = new Test();
t.Test();
}
}
✅ Output:
Constructor
This is a method
📌 Why? Because method name can be same as class name, but it still acts like a method due to return type.
🔸 Puzzle 2:
class Demo {
Demo() {
this(10);
System.out.println("Default");
}
Demo(int x) {
System.out.println("Parameterized: " + x);
}
public static void main(String[] args) {
new Demo();
}
}
✅ Output:
Parameterized: 10
Default
🔸 Puzzle 3:
class A {
A() {
System.out.println("A constructor");
}
}
class B extends A {
B() {
System.out.println("B constructor");
}
}
public class Main {
public static void main(String[] args) {
new B();
}
}
✅ Output:
A constructor
B constructor
📌 Reason: Super constructor is always called first.
✨ Summary
Keyword | Use |
---|---|
this() |
Call constructor in same class |
super() |
Call parent class constructor |
Overloading | Yes, constructors can be overloaded |
Inheritance | Constructors are not inherited |