Java
Constructors in Java
- Last updated Apr 25, 2024
In Java, constructors are like special methods that have the same name as the class but do not have a return type. They are used for initializing fields or objects and are invoked when an object is created.
Types of Constructors
There are two types of constructors in Java:
- Default Constructors:
- Parameterized Constructors:
A default constructor is a constructor without parameters. Every Java class has a public default constructor by default, meaning even if you do not explicitly define any constructors, Java provides a default public constructor with no parameters. This default constructor of a class is called when you create an object of that class using the new keyword without passing parameters. The default constructor initializes the object with default values, which are often zero for numeric types and null for reference types. For example:
public class Car {
// Default constructor
public Car() {
System.out.println("A new car is created with default values.");
}
// Other methods and attributes can be added here
}
public class Main {
public static void main(String[] args) {
// Creating an object of the Car class invokes the default constructor
Car myCar = new Car();
}
}
Parameterized constructors in Java are constructors with parameters. These are custom constructors that we can create to accept parameters, allowing the initialization of object attributes with specific values during object creation. For example:
public class Student {
private String firstName;
private String lastName;
private int age;
// Parameterized constructor
public Student(String firstName, String lastName, int age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
System.out.println("A new student is created with name: " + firstName + " " + lastName + " and age: " + age);
}
// Other methods and attributes can be added here
}
public class Main {
public static void main(String[] args) {
// Creating an object of the Student class with parameters
Student myStudent = new Student("Danny", "Mac", 28);
}
}
Overloading Constructors
In Java, constructor overloading is a feature that allows a class to have multiple constructors, each with a different parameter list. In other words, you can define several constructors within a class, and each constructor can take a different set of parameters. When you create an object of that class, you can choose which constructor to use based on the parameters you want to provide.
class Rectangle {
private int height;
private int width;
// Parameterized constructor
public Rectangle(int height, int width) {
this.height = height;
this.width = width;
}
// Overloaded constructor with a default width
public Rectangle(int height) {
this.height = height;
this.width = 5; // Default width
}
}
In this example, the Rectangle class has two constructors. The first is a parameterized constructor that takes both height and width, while the second is an overloaded constructor that allows creating a rectangle with only the height, assuming a default width.
Chaining Constructors
Constructor chaining involves calling one constructor from another within the same class. This promotes code reuse and helps avoid redundancy. Here's a simple example:
class Person {
private String name;
private int age;
// Parameterized constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Constructor chaining using "this"
public Person(String name) {
this(name, 28); // Default age
}
}
In this example, the parameterized constructor of the Person class chains to another constructor using this(name, 28), ensuring that if only the name is provided, the default age is set.
Initialization Order
Understanding the order in which constructors are invoked is important, especially when dealing with inheritance. In Java, subclass constructors implicitly call the superclass constructor first. Let's illustrate this with a simple example:
class Vehicle {
public Vehicle() {
System.out.println("Vehicle constructor is invoked.");
}
}
class Car extends Vehicle {
public Car() {
System.out.println("Car constructor is invoked.");
}
}
public class Main {
public static void main(String[] args) {
// Creating an object of the Car class
Car myCar = new Car();
}
}
In this example, when you create a Car object, the constructor of the Vehicle superclass is invoked before the Car constructor. The output is as follows:
Vehicle constructor is invoked. Car constructor is invoked.
Important Points to Remember
- A Constructor is not considered as a member of a class because constructors are not inherited by subclasses in the same way regular methods are. While a class inherits methods and fields from its superclass, constructors are not inherited. Each class must define its own constructors.