Menu
Coddy logo textTech

Constructor Overloading

Part of the Object Oriented Programming section of Coddy's Java journey — lesson 8 of 87.

Constructor overloading means having multiple constructors with different parameter lists. Use this() to call one constructor from another.

Multiple constructors

public class Book {
    private String title;
    private String author;
    private int pages;
    
    public Book(String title, String author, int pages) {
        this.title = title;
        this.author = author;
        this.pages = pages;
    }
    
    public Book(String title, String author) {
        this.title = title;
        this.author = author;
        this.pages = 0;
    }
}

Constructor chaining with this()

public class Book {
    private String title;
    private String author;
    private int pages;
    
    public Book(String title, String author, int pages) {
        this.title = title;
        this.author = author;
        this.pages = pages;
    }
    
    public Book(String title, String author) {
        this(title, author, 0);  // Calls 3-param constructor
    }
    
    public Book() {
        this("Unknown", "Unknown");  // Calls 2-param constructor
    }
}

Usage

Book b1 = new Book("1984", "Orwell", 328);
Book b2 = new Book("Dune", "Herbert");
Book b3 = new Book();

this() must be the first statement in the constructor. Chaining avoids duplicating assignment logic across constructors.

challenge icon

Challenge

Medium

Create three overloaded constructors for the Product class using constructor chaining:

  • 3-parameter constructor: name, price, stock
  • 2-parameter constructor: name, price (stock defaults to 0)
  • Default constructor: no parameters (name = "Unknown", price = 0, stock = 0)

Use this() to chain constructors and avoid code duplication.

Cheat sheet

Constructor overloading allows multiple constructors with different parameter lists in the same class.

Multiple constructors example:

public class Book {
    private String title;
    private String author;
    private int pages;
    
    public Book(String title, String author, int pages) {
        this.title = title;
        this.author = author;
        this.pages = pages;
    }
    
    public Book(String title, String author) {
        this.title = title;
        this.author = author;
        this.pages = 0;
    }
}

Constructor chaining with this() calls one constructor from another to avoid code duplication:

public class Book {
    private String title;
    private String author;
    private int pages;
    
    public Book(String title, String author, int pages) {
        this.title = title;
        this.author = author;
        this.pages = pages;
    }
    
    public Book(String title, String author) {
        this(title, author, 0);  // Calls 3-param constructor
    }
    
    public Book() {
        this("Unknown", "Unknown");  // Calls 2-param constructor
    }
}

Usage:

Book b1 = new Book("1984", "Orwell", 328);
Book b2 = new Book("Dune", "Herbert");
Book b3 = new Book();

Important: this() must be the first statement in the constructor.

Try it yourself

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        double price = Double.parseDouble(scanner.nextLine());
        int stock = Integer.parseInt(scanner.nextLine());
        
        Product product1 = new Product(name, price, stock);
        Product product2 = new Product("Keyboard", 79.99);
        Product product3 = new Product();
        
        System.out.println("Product 1: " + product1.getName() + " - $" + product1.getPrice() + " (Stock: " + product1.getStock() + ")");
        System.out.println("Product 2: " + product2.getName() + " - $" + product2.getPrice() + " (Stock: " + product2.getStock() + ")");
        System.out.println("Product 3: " + product3.getName() + " - $" + product3.getPrice() + " (Stock: " + product3.getStock() + ")");
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming