Skip to main content

6.6 Constructing an Object, Calling a Constructor from a Constructor

1. Introducing the Module and Its Agenda

In this module, we will learn about object construction in Java, focusing on how constructors are used to initialize objects. We will also explore how to call one constructor from another using constructor chaining and how to call the superclass constructor. By the end of this module, you will have a solid understanding of constructor behavior in Java.


2. Creating or Generating a Constructor in a Class

A constructor is a special method used to initialize objects. It has the same name as the class and does not have a return type. If you do not provide a constructor, Java automatically generates a default one. You can also create parameterized constructors to initialize object fields.

Example:

public class Person {
private String name;
private int age;

// Constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}

In this example, the Person class has a constructor that takes two parameters to initialize the name and age fields.


3. Calling a Constructor from a Constructor

In Java, you can call one constructor from another within the same class using the this() keyword. This is known as constructor chaining and helps avoid code duplication.

Example:

public class Car {
private String model;
private int year;

// Constructor 1
public Car(String model) {
this(model, 2020); // Calls Constructor 2
}

// Constructor 2
public Car(String model, int year) {
this.model = model;
this.year = year;
}
}

In this example, Constructor 1 calls Constructor 2 using the this() keyword.


4. Live Demo: Setting a Final Variable through a Constructor

Final variables can only be assigned once, either at the time of declaration or inside a constructor. Let's see how to set a final variable through a constructor.

Example:

public class Book {
private final String title;

public Book(String title) {
this.title = title; // Final variable set through constructor
}
}

The title variable is set once in the constructor and cannot be changed afterward.


5. Live Demo: Calling a Constructor from Another Constructor

This demo showcases constructor chaining, where one constructor calls another within the same class to avoid repetitive code.

Example:

public class Student {
private String name;
private int age;

public Student(String name) {
this(name, 18); // Calls another constructor
}

public Student(String name, int age) {
this.name = name;
this.age = age;
}
}

6. Live Demo: Calling a Super Constructor from a Constructor

You can call a superclass constructor from a subclass using the super() keyword. This is useful when you want to initialize fields in the superclass.

Example:

class Animal {
private String name;

public Animal(String name) {
this.name = name;
}
}

class Dog extends Animal {
public Dog(String name) {
super(name); // Calls the superclass constructor
}
}

In this example, the Dog class calls the constructor of the Animal class using super().


We’ve covered constructors, constructor chaining, and calling superclass constructors. Constructors are crucial for initializing objects, and learning how to efficiently manage them will lead to better structured and maintainable code. Keep practicing constructor chaining and super() calls to gain a deeper understanding. Thank you for participating in this module!