Constructor Overloading in Java Example w3schools Tutorial Guide

Robert Jul 13, 2026

Constructor overloading is a powerful feature in Java that enables you to define multiple constructors within a class. This allows for increased flexibility and customization when creating objects, as each constructor can accept different parameters. But how exactly does it work? Let's dive in, using examples from W3Schools to guide us.

Constructor Overloading in Java | Example Program - Scientech Easy
Constructor Overloading in Java | Example Program - Scientech Easy

Before we proceed, let's ensure we have the basics right. A constructor in Java is a special method that's automatically called when an object is created. It's used to initialize the object's attributes and prepare it for use. Now, let's consider constructor overloading in Java.

Method Overloading And Method Overriding In Java
Method Overloading And Method Overriding In Java

Constructor Overloading Syntax

The syntax for constructor overloading involves defining multiple constructors within the same class, with each constructor having a unique signature (i.e., a different number of parameters, or parameters of different types). Here's a simple example from W3Schools:

Difference between Method and Constructor in Java and OOP? Example
Difference between Method and Constructor in Java and OOP? Example

Syntax: public class ClassName {
     public ClassName(paramType1 paramName1) {...}
     public ClassName(paramType2 paramName2) {...}
}

Overloaded Constructors with Different Number of Parameters

Can You Overload or Override Static methods in Java? Example
Can You Overload or Override Static methods in Java? Example

Let's look at an example from W3Schools that demonstrates constructors with different numbers of parameters:

Example: public class Car {
     public Car() {...}
     public Car(String model) {...}
     public Car(String model, int year) {...}
}

In this example, the Car class has three constructors: one with no parameters, one with a string parameter for the model, and one with a string and an int for the model and year.

Main Difference Between Overloading And Overriding in Java
Main Difference Between Overloading And Overriding in Java

Overloaded Constructors with Different Parameter Types

Constructors can also be overloaded with different types of parameters. Here's an example from W3Schools:

Example: public class Car {
     public Car(String model) {...}
     public Car(int year) {...}
     public Car(String model, int year) {...}
}

Java Constructors Explained | Easy Cheat Sheet for Beginners
Java Constructors Explained | Easy Cheat Sheet for Beginners

In this case, the Car class has three constructors. The first accepts a string for the model, the second an int for the year, and the third both a string for the model and an int for the year.

Constructor Chaining and this() Method

Polymorphism in Java Explained | Method Overloading & Overriding Easy Notes
Polymorphism in Java Explained | Method Overloading & Overriding Easy Notes
Java Best Practices for Method Overloading? Examples
Java Best Practices for Method Overloading? Examples
Difference between Method and constructor in Java - Codeforcoding
Difference between Method and constructor in Java - Codeforcoding
Method overloading in Java
Method overloading in Java
Constructor in Java with Example | Java Constructor Definition, Types, Rules, Declarations - BTech Geeks
Constructor in Java with Example | Java Constructor Definition, Types, Rules, Declarations - BTech Geeks
Explore Method Overloading vs Overriding in Java
Explore Method Overloading vs Overriding in Java
Constructors In Java – Three Different Types Of Constructors
Constructors In Java – Three Different Types Of Constructors
What is Constructor in Java and How it works? [with Example]
What is Constructor in Java and How it works? [with Example]
Constructors in Java- Types and Examples
Constructors in Java- Types and Examples
30 Days of Java – Day 15 | Java Polymorphism Explained for Beginners
30 Days of Java – Day 15 | Java Polymorphism Explained for Beginners
an electrical wiring diagram with two different types of wires and the corresponding connections to each other
an electrical wiring diagram with two different types of wires and the corresponding connections to each other
Checkout Method Overriding in Java with Rules and Real-time Examples
Checkout Method Overriding in Java with Rules and Real-time Examples
30 Days of Java – Day 13 | Java Constructors Explained for Beginners
30 Days of Java – Day 13 | Java Constructors Explained for Beginners
The OOP'S Concept in JAVA🤔💯✅👇
The OOP'S Concept in JAVA🤔💯✅👇
Best Java Training in Chennai|Java Course Chennai - Enroll Now
Best Java Training in Chennai|Java Course Chennai - Enroll Now
the cover of an ebook with text that reads, increment & decrement operators
the cover of an ebook with text that reads, increment & decrement operators
Logical Operators in Java: Think Smarter, Code Better ☕🚀
Logical Operators in Java: Think Smarter, Code Better ☕🚀
Must-Have IntelliJ IDEA Plugins For Java Developers
Must-Have IntelliJ IDEA Plugins For Java Developers
the cover for comparison operators true or false?
the cover for comparison operators true or false?
All You Need to Know About Java Constructor
All You Need to Know About Java Constructor

Constructor overloading also allows for constructor chaining, where one constructor calls another constructor within the same class. This is done using the this() method. Let's see an example from W3Schools:

Example: public class Car {
     public Car() {
         this("Unknown model");
     }
     public Car(String model) {...}
}

Constructors with this() Method

In this example, the first constructor without any parameters calls the second constructor with a string parameter using this("Unknown model"). This allows us to reuse initialization code and keep our constructors DRY (Don't Repeat Yourself).

Constructors with this() Method - Chaining

We can also chain constructor calls with this(). For instance, we might have a constructor with three parameters, and want to call another constructor with two parameters. Here's how we might do it:

Example: public class Car {
     public Car() {...}
     public Car(String model, int year) {...}
     public Car(String model, int year, String color) {
         this(model, year);
         this.color = color;
     }
}

In this final example, the third constructor calls the second constructor with this(model, year) and then sets the color attribute. This demonstrates how we can use this() to implement complex initialization logic more efficiently.

In conclusion, constructor overloading in Java provides a powerful way to create flexible objects. By defining multiple constructors with different parameters, you can support a variety of use cases and make your classes easier to use. Moreover, by using the this() method, you can simplify your code and avoid repetition. So, why not give it a try in your next project?