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 |
Top comments (0)