Java Constructor
Q1. How can a Java class support both default and parameterized object creation ?
A Java class can support both default and customized object creation by defining multiple constructors.
Example
Using both constructors allows objects to be created with or without initial values.
- A default constructor initializes objects with default values.
- A parameterized constructor allows values to be passed at the time of object creation.
Example
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); }}Using both constructors allows objects to be created with or without initial values.
Q2. What is a copy constructor in Java, and why is it used ?
A copy constructor is a constructor that takes another object of the same class as a parameter and copies its data members.
Although Java does not provide a built-in copy constructor, it can be implemented manually.
Why it is used
Example
Both objects contain the same values but occupy different memory locations.
Although Java does not provide a built-in copy constructor, it can be implemented manually.
Why it is used
- To create a separate object with the same data
- To avoid shared references
- To ensure object independence
Example
class Employee { String name; int id;
Employee(String name, int id) { this.name = name; this.id = id; }
// Copy constructor Employee(Employee e) { this.name = e.name; this.id = e.id; }}Both objects contain the same values but occupy different memory locations.
Q3. What is constructor chaining in Java ?
Constructor chaining is the process of calling one constructor from another constructor within the same class using this().
It helps:
Example
Here, the constructor with one parameter delegates initialization to another constructor.
It helps:
- Reduce code duplication
- Improve maintainability
- Centralize initialization logic
Example
class Student { String name; int age;
Student(String name) { this(name, 18); // Calls parameterized constructor }
Student(String name, int age) { this.name = name; this.age = age; }}Here, the constructor with one parameter delegates initialization to another constructor.
Q4. What is the difference a constructor and a method in Java ?
Java distinguishes constructors from methods based on syntax and behavior.
| Constructor | Method |
|---|---|
| Has the same name as the class | Can have any valid name |
| Does not have a return type | Must have a return type |
| Called automatically during object creation | Called explicitly |
| Used to initialize objects | Used to perform operations |
Example
Even though the method name matches the class name, the presence of a return type makes it a normal method.
class Demo {
Demo() { // Constructor System.out.println("Constructor called"); }
void Demo() { // Method System.out.println("Method called"); }}Even though the method name matches the class name, the presence of a return type makes it a normal method.