OOP · Core Java OOP Features
Constructor Overloading & Chaining
Imagine you're building an Online Shopping System.
Constructor Overloading
Real-World Problem
Imagine you're building an Online Shopping System. Customers can register in different ways:
- Only with a username.
- With a username and email.
- With a username, email, and phone number.
Using a single constructor cannot handle all these situations. We need multiple constructors to initialize objects based on the available information.

Why Do We Need Constructor Overloading? Different objects may require different initialization data. Instead of creating separate methods for every scenario, Java allows multiple constructors with different parameter lists. This makes object creation more flexible and improves code readability. What is Constructor Overloading?
Constructor overloading is the process of defining multiple constructors in the same class with different parameter lists. Java automatically calls the constructor that matches the arguments passed during object creation.


Constructor Chaining
Real-World Problem
Imagine a Student Registration System. Every student has:

- Name
- Age
- Department
If every constructor initializes these fields separately, the same code is repeated multiple times. As the application grows, maintaining repeated initialization becomes difficult. Java solves this using Constructor Chaining. Why Do We Need Constructor Chaining?
Constructor chaining helps one constructor reuse another constructor instead of rewriting the same initialization code. It reduces duplication, improves readability, and makes maintenance easier. What is Constructor Chaining? Constructor chaining is the process of invoking one constructor from another constructor. It can be achieved using:
this()→ calls another constructor in the same class.super()→ calls the parent class constructor.
Rules of Constructor Chaining
this()must be the first statement in a constructor.- Only one
this()call is allowed in a constructor. - Constructor chaining avoids duplicate initialization code.
this()andsuper()cannot be used together in the same constructor because both must be the first statement.- If
this()is not used, Java implicitly insertssuper()as the first statement
Advantages of Constructors
- Automatically initializes objects.
- Reduces repetitive initialization code.
- Improves code readability.
- Ensures objects start with valid data.
- Supports constructor overloading and chaining.
21 this Keyword
Real-World Problem
Imagine a Student Management System where the constructor parameter names are the same as the instance variable names. Without a way to distinguish them, Java cannot determine whether you're referring to the local variable or the object's variable. Why Do We Need this?
The this keyword helps Java refer to the current object. It is commonly used to remove naming conflicts, call another constructor, invoke current class methods, and pass or return the current object. What is this? The this keyword is a reference variable that refers to the current object of the class.
Uses of this
- Refer current object variables
- Invoke current class methods
- Invoke current constructor (
this()) - Pass current object as an argument
- Return current object
Provide several constructors and chain them with this(...) to avoid duplicated setup.
public class Rectangle {
private final int width, height;
public Rectangle(int width, int height) { // primary constructor
this.width = width;
this.height = height;
}
public Rectangle(int side) {
this(side, side); // chaining — delegates to the primary constructor
}
public Rectangle() {
this(1); // chains again — a 1x1 unit square
}
}