Java Interview Questions
Java
Web DevelopmentBackendQuestion 13
What is a constructor in Java?
Answer:
In Java, a constructor is a special method that is called when an object is instantiated. It is used to initialize the object and set up its initial state. Constructors are named the same as the class and do not have a return type, not even void
.
Key Characteristics of Constructors
- Name: The constructor's name must be the same as the class name.
- No Return Type: Constructors do not have a return type, not even
void
. - Automatic Call: A constructor is automatically called when an instance of the class is created.
- Initialization: The primary purpose of a constructor is to initialize the object's fields.
Types of Constructors
- Default Constructor
- Parameterized Constructor
Default Constructor
If no constructor is explicitly defined in a class, the Java compiler automatically provides a no-argument constructor, known as the default constructor. It initializes the object with default values (e.g., 0
for integers, null
for objects).
Example:
public class Car {
String model;
int year;
// Default constructor
public Car() {
this.model = "Unknown";
this.year = 0;
}
public void display() {
System.out.println("Model: " + model + ", Year: " + year);
}
public static void main(String[] args) {
Car car = new Car(); // Default constructor is called
car.display(); // Output: Model: Unknown, Year: 0
}
}
Parameterized Constructor
A parameterized constructor allows you to provide different values for object fields when the object is created. This type of constructor takes arguments to initialize the fields of the object with specific values.
Example:
public class Car {
String model;
int year;
// Parameterized constructor
public Car(String model, int year) {
this.model = model;
this.year = year;
}
public void display() {
System.out.println("Model: " + model + ", Year: " + year);
}
public static void main(String[] args) {
Car car1 = new Car("Toyota", 2020); // Parameterized constructor is called
Car car2 = new Car("Honda", 2018);
car1.display(); // Output: Model: Toyota, Year: 2020
car2.display(); // Output: Model: Honda, Year: 2018
}
}
Overloading Constructors
You can have multiple constructors in a class with different parameter lists. This is known as constructor overloading, and it allows you to create objects in different ways.
Example:
public class Car {
String model;
int year;
// Default constructor
public Car() {
this.model = "Unknown";
this.year = 0;
}
// Parameterized constructor
public Car(String model, int year) {
this.model = model;
this.year = year;
}
public void display() {
System.out.println("Model: " + model + ", Year: " + year);
}
public static void main(String[] args) {
Car car1 = new Car(); // Default constructor is called
Car car2 = new Car("Ford", 2021); // Parameterized constructor is called
car1.display(); // Output: Model: Unknown, Year: 0
car2.display(); // Output: Model: Ford, Year: 2021
}
}
Key Points
- Constructor Chaining: Constructors can call other constructors in the same class using
this()
or from the superclass usingsuper()
. This is known as constructor chaining. - Inheritance: Constructors are not inherited by subclasses. However, a subclass constructor can call a superclass constructor using the
super
keyword. - Access Modifiers: Constructors can have access modifiers to control the instantiation of objects. For instance, a private constructor can prevent the creation of instances from outside the class, which is useful in implementing the Singleton pattern.
Constructor Chaining Example
Example:
public class Vehicle {
String type;
// Constructor
public Vehicle(String type) {
this.type = type;
}
}
public class Car extends Vehicle {
String model;
int year;
// Constructor
public Car(String type, String model, int year) {
super(type); // Calling the constructor of the superclass
this.model = model;
this.year = year;
}
public void display() {
System.out.println("Type: " + type + ", Model: " + model + ", Year: " + year);
}
public static void main(String[] args) {
Car car = new Car("Sedan", "Toyota", 2020);
car.display(); // Output: Type: Sedan, Model: Toyota, Year: 2020
}
}
In summary, constructors are special methods used to initialize objects in Java. They provide a way to set up initial values for object fields and ensure that the object is in a valid state when it is created. Understanding how to use constructors effectively is essential for creating robust and maintainable Java applications.