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
MediumCreate 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() + ")");
}
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesIntroduction to OOPClasses vs ObjectsThe this KeywordMethodsFields (Attributes)Constructor MethodConstructor OverloadingRecap - Simple Calculator4Inheritance
Basic Inheritance (extends)The super KeywordMethod Overriding (@Override)Constructor ChainingThe Object ClassSingle & Multilevel InheritWhy No Multi Class InheritRecap - Employee Hierarchy7Special Methods & Object Class
toString() Methodequals() and hashCode()clone() MethodcompareTo() and ComparableComparator InterfaceRecap - Custom Sorting2Access Modifiers & Encapsulate
Access Levels OverviewGetter and Setter MethodsInformation HidingThe final KeywordRecap - Bank Account Manager5Polymorphism
Method Overloading BasicsMethod Overriding (Run-Time)Upcasting and DowncastingThe instanceof OperatorAbstract Classes and MethodsRecap - Shape Calculator8Advanced OOP Concepts
Composition vs InheritanceAggregation vs CompositionInner Nested & Anonymous ClassEnums and Enum MethodsRecords (Java 16+)Sealed Classes (Java 17+)3Class Props & Static Member
Instance vs Static VariablesStatic MethodsStatic BlocksConstants (static final)Recap - Counter & Utility6Interfaces & Abstract Classes
Introduction to InterfacesImplementing InterfacesMulti Interface ImplemenDefault & Static in InterfaceAbstract Classes vs InterfacesFunctional InterfacesRecap - Payment System