Java OOP · Module 3
Multiple constructors
Overloading, sensible defaults, and chaining with this(...)
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
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?
| Call | Chosen constructor | Why |
|---|---|---|
| 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 error | no constructor takes a single int |
Add a constructor `Pizza(String size, String topping, int price)` and order a custom pizza with it.
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
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
Which message prints first? Predict, then run. Then move `this(...)` below the println and read the error.
`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
Pizza(String size) { } and Pizza(String topping) { }Pizza() { System.out.println("building"); this("medium"); }Copying the same field assignments into all three constructors.Lesson playground
A Rectangle with two constructors: one for rectangles, one for squares. Add a no-argument constructor that builds a 1x1 unit square using this(...).
Check understanding
Quiz
Question 1 of 5
Which constructor runs for each call, and what is printed?
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.