← Back to Week 2 Hub

Constructor Overloading

Same class name, different parameter lists — Java picks the right constructor based on your arguments.

The Movie Class
public class Movie { private String title; private String director; private int year; private double rating; }
Constructor 1 — No Arguments (Defaults)
public Movie() {
    this.title = "Unknown";
    this.director = "Unknown";
    this.year = 0;
    this.rating = 0.0;
}
Constructor 2 — Title and Year
public Movie(String title, int year) {
    this.title = title;
    this.year = year;
    this.director = "Unknown";
    this.rating = 0.0;
}
Constructor 3 — All Fields
public Movie(String title, String director, int year, double rating) {
    this.title = title;
    this.director = director;
    this.year = year;
    this.rating = rating;
}
Create a Movie

No arguments needed — all fields will use default values.

Created Object
FieldValueSource
How Java Picks the Right Constructor

Java matches by the number and types of arguments you pass. Click each row to reveal the match.

new Movie() 0 args Constructor 1 0 arguments = no-arg constructor
new Movie("Inception", 2010) String, int Constructor 2 String + int matches (String title, int year)
new Movie("The Dark Knight", "Nolan", 2008, 9.0) String, String, int, double Constructor 3 4 args with matching types = full constructor
Tip: Overloading lets you create objects in different ways. Use the no-arg constructor when you will set values later with setters. Use the parameterized constructor when you have all the data upfront.

Click a constructor button above to see which constructor Java calls and how the object is built.

← Encapsulation: public vs private Method Overloading →