Skip to content

Java OOP

0/36 lessons · 0% complete

— XP

Java OOP · Module 3

Multiple constructors

Overloading, sensible defaults, and chaining with this(...)

20m55 XP
→→→

What you'll be able to do

  • Give a class several constructors with different parameter lists
  • Predict which constructor a `new` call selects
  • Remove duplication by chaining constructors with `this(...)`
  • Recognise why two constructors with the same parameter types cannot coexist
1

Constructor Overloading

Several ways to build the same object

A pizza order can be 'the usual', 'large', or 'small with olives'. One class, three constructors.

Constructor overloading means declaring more than one constructor in the same class. They all share the class name, so Java tells them apart by their parameter list: how many parameters, of which types, in which order. The compiler picks the matching one at the new call — this decision happens at *compile time*, which is why it is called compile-time polymorphism (Module 9). The rule to remember: the parameter list must differ. Changing only the parameter *names* changes nothing, because names are not part of the signature.

Which constructor runs?

CallChosen constructorWhy
new Pizza()Pizza()no arguments
new Pizza("large")Pizza(String)one String argument
new Pizza("small", "olives")Pizza(String, String)two String arguments
new Pizza(2)none → compile errorno constructor takes a single int
Three constructors, three orders

Add a constructor `Pizza(String size, String topping, int price)` and order a custom pizza with it.

Output
Press Run to execute this example.

Same types = same signature

Pizza(String size) and Pizza(String topping) cannot coexist: both are Pizza(String). The compiler reports 'constructor Pizza(String) is already defined'. If you truly need both, change the design — for example a static factory method with a clear name.

Prove it — coding challenges

2

The this Keyword

Chaining constructors with this(...)

Copy-pasting the same three assignments into every constructor is how bugs get introduced. `this(...)` fixes it.

Look again at the Pizza class: the three constructors repeat the same assignments with different values. If tomorrow a discount field appears, you must remember to update all three. this(...) lets one constructor delegate to another. One constructor — usually the one with the most parameters — owns the real initialisation, and the shorter ones just fill in defaults and call it. Two rules: 1. The this(...) call must be the very first statement in the constructor. 2. A constructor cannot chain to itself (that would be infinite recursion, and the compiler says so).

One owner, two shortcuts

Main.java
1Pizza(String size, String topping, int price) {
2 this.size = size;
3 this.topping = topping;
4 this.price = price;
5}
6
7Pizza(String size) {
8 this(size, "cheese", 70);
9}
10
11Pizza() {
12 this("medium", "cheese", 60);
13}
Pizza(String size, String topping, int price) {
The primary constructor: the only place that actually assigns fields.
this(size, "cheese", 70);
Delegates to the primary constructor, supplying defaults for what the caller did not provide. No assignment is duplicated.
this("medium", "cheese", 60);
Same idea with all defaults. Add a field later and you edit exactly one constructor.
See the delegation order

Which message prints first? Predict, then run. Then move `this(...)` below the println and read the error.

Output
Press Run to execute this example.

`this(...)` vs `this.field`

this.name = *my* field. this(...) = *another constructor of my own class*. In Module 6 you will meet the sibling super(...), which calls the parent class constructor — and follows the same 'first statement' rule.

Prove it — coding challenges

Common mistakes

WrongPizza(String size) { } and Pizza(String topping) { }
WhyIdentical signature `Pizza(String)` — parameter names are not part of the signature.
FixDiffer by number, type or order of parameters, or use differently named factory methods.
WrongPizza() { System.out.println("building"); this("medium"); }
Why`this(...)` must be the first statement in the constructor.
FixMove the delegation to the first line, and print afterwards.
WrongCopying the same field assignments into all three constructors.
WhyEvery future change must be made three times — and one will be forgotten.
FixChain with `this(...)` so only one constructor assigns fields.

Lesson playground

Free practice

A Rectangle with two constructors: one for rectangles, one for squares. Add a no-argument constructor that builds a 1x1 unit square using this(...).

Output
Press Run to execute this example.

Check understanding

Quiz

Question 1 of 5

Which constructor runs for each call, and what is printed?

Main.java
1class Ticket {
2 String type;
3 int price;
4
5 Ticket() {
6 this("standard", 100);
7 }
8
9 Ticket(String type) {
10 this(type, 150);
11 }
12
13 Ticket(String type, int price) {
14 this.type = type;
15 this.price = price;
16 }
17
18 void print() {
19 System.out.println(type + ": " + price);
20 }
21}
22
23public class Main {
24 public static void main(String[] args) {
25 Ticket a = new Ticket();
26 Ticket b = new Ticket("vip");
27 Ticket c = new Ticket("student", 40);
28
29 a.print();
30 b.print();
31 c.print();
32 }
33}

Summary

  • Overloaded constructors differ by number, type or order of parameters — never by names alone.
  • Java chooses the constructor at compile time from the argument list.
  • `this(...)` delegates to another constructor and must be the first statement.
  • Let one primary constructor own the assignments; the others only supply defaults.