Java Constructor
Interview Questions of constructor
Scenario 1: Default vs. Parameterized Constructor
Scenario:
You are building a Book class for a library management system. Sometimes, you need to create a book with default values, and other times, you want to set the title and author while creating the object.
Question:
How would you design the Book class to handle both situations? Write the constructors and explain your choice.
Answer:
I would provide both a default constructor and a parameterized constructor. The default constructor sets default values, while the parameterized constructor allows custom values.
Output:
This design provides flexibility for object creation
Output:
This ensures both objects have the same data but are independent in memory.
Output:
Constructor chaining improves maintainability and reduces code repetition.
Output:
Java distinguishes constructors by the absence of a return type.
Scenario:
You are building a Book class for a library management system. Sometimes, you need to create a book with default values, and other times, you want to set the title and author while creating the object.
Question:
How would you design the Book class to handle both situations? Write the constructors and explain your choice.
Answer:
I would provide both a default constructor and a parameterized constructor. The default constructor sets default values, while the parameterized constructor allows custom values.
class Book { String title; String author;
// Default constructor Book() { this.title = "Unknown"; this.author = "Unknown"; }
// Parameterized constructor Book(String title, String author) { this.title = title; this.author = author; }
void display() { System.out.println("Title: " + title + ", Author: " + author); }
public static void main(String[] args) { Book b1 = new Book(); Book b2 = new Book("Java Basics", "Alice"); b1.display(); b2.display(); }}
Output:
Title: Unknown, Author: Unknown
Title: Java Basics, Author: Alice
This design provides flexibility for object creation
Scenario 2: Copy Constructor
Scenario:
You have an Employee object, and you want to create a duplicate of this object with the same data.
Question:
How would you implement a copy constructor in Java, and why might you need one?
Answer:
A copy constructor takes another object of the same class as a parameter and copies its fields. This is useful for creating independent copies of objects.
Scenario:
You have an Employee object, and you want to create a duplicate of this object with the same data.
Question:
How would you implement a copy constructor in Java, and why might you need one?
Answer:
A copy constructor takes another object of the same class as a parameter and copies its fields. This is useful for creating independent copies of objects.
class Employee { String name; int id;
// Parameterized constructor Employee(String name, int id) { this.name = name; this.id = id; }
// Copy constructor Employee(Employee e) { this.name = e.name; this.id = e.id; }
void show() { System.out.println("Name: " + name + ", ID: " + id); }
public static void main(String[] args) { Employee e1 = new Employee("Bob", 101); Employee e2 = new Employee(e1); // Copy constructor e1.show(); e2.show(); }}
Output:
Name: Bob, ID: 101
Name: Bob, ID: 101
This ensures both objects have the same data but are independent in memory.
Scenario 3: Constructor Chaining
Scenario:
Scenario:
You want to avoid code duplication when initializing a Student object. Sometimes, you want to create a student with only a name, and other times with both name and age.
Question:
How would you use constructor chaining to achieve this?
Answer:
I would use this() to call one constructor from another, reducing code duplication.
Question:
How would you use constructor chaining to achieve this?
Answer:
I would use this() to call one constructor from another, reducing code duplication.
class Student { String name; int age;
// Constructor with only name Student(String name) { this(name, 18); // Default age }
// Constructor with name and age Student(String name, int age) { this.name = name; this.age = age; }
void print() { System.out.println("Name: " + name + ", Age: " + age); }
public static void main(String[] args) { Student s1 = new Student("Alice"); Student s2 = new Student("Bob", 21); s1.print(); s2.print(); }}
Output:
Name: Alice, Age: 18
Name: Bob, Age: 21
Constructor chaining improves maintainability and reduces code repetition.
Scenario 4: Constructor vs. Method
Scenario:
Scenario:
A new developer on your team writes a method with the same name as the class but adds a return type.
Question:
What will happen in this case? How does Java distinguish between a constructor and a method?
Answer:
If a method has a return type, it is treated as a regular method, not a constructor. Constructors must not have a return type. If no constructor is defined, Java provides a default one.
Question:
What will happen in this case? How does Java distinguish between a constructor and a method?
Answer:
If a method has a return type, it is treated as a regular method, not a constructor. Constructors must not have a return type. If no constructor is defined, Java provides a default one.
class Demo { Demo() { // This is a constructor System.out.println("Constructor called"); }
void Demo() { // This is a method, not a constructor System.out.println("Method called"); }
public static void main(String[] args) { Demo d = new Demo(); // Calls constructor d.Demo(); // Calls method }}
Output:
Constructor called
Method called
Java distinguishes constructors by the absence of a return type.