DEV Community

Vigneshwaralingam
Vigneshwaralingam

Posted on

πŸš€ Java Constructors – Masterclass with Examples

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(); } } 
Enter fullscreen mode Exit fullscreen mode

🧠 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); } } 
Enter fullscreen mode Exit fullscreen mode

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 } 
Enter fullscreen mode Exit fullscreen mode

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); } 
Enter fullscreen mode Exit fullscreen mode

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(); } } 
Enter fullscreen mode Exit fullscreen mode

βœ… Output:

Constructor This is a method 
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ 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(); } } 
Enter fullscreen mode Exit fullscreen mode

βœ… Output:

Parameterized: 10 Default 
Enter fullscreen mode Exit fullscreen mode

πŸ”Έ 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(); } } 
Enter fullscreen mode Exit fullscreen mode

βœ… Output:

A constructor B constructor 
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ 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)