Skip to main content

Constructor Overloading Example in Java

2 min read Updated May 29, 2026
Share:
On this page (10sections)

Introduction

Constructor Overloading is a classic Java console program that demonstrates the concept with complete source code and sample output. These programs cover your first Java class, constructors, methods and simple OOP building blocks.

This tutorial walks through the program line by line, explains how the logic works, and highlights best practices you can apply in your own code.

Definition

A constructor in a class is a special type of subroutine called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required member variables. A constructor resembles an instance method, but it differs from a method in that it has no explicit return type. Constructors often have the same name as the declaring class.

Syntax

NameOfClass() {
    //Statements
    }
NameOfClass(Parameters) {
    //Statements
    }
ParentClass{
	//Statements
	}

Constructor Overloading Example Program

class ConstructorOverloading{
	ConstructorOverloading(){
		System.out.println("1. " + "Constructor with no parameter.");
	}
	ConstructorOverloading(int p, int q){
		System.out.println("2. " + (p+q));
	}
	ConstructorOverloading(int p, int q, int r){
		System.out.println("3. " + (p+q+r));
	}
	ConstructorOverloading(int p, int q, float r){
		System.out.println("4. " + (p+q+r));
	}
	ConstructorOverloading(float r, int p, int q){
		System.out.println("5. " + (p+q+r));
	}
	public static void main(String []args){
		ConstructorOverloading obj = new ConstructorOverloading(10, 20);
		ConstructorOverloading obj1 = new ConstructorOverloading(10, 20, 30);
		ConstructorOverloading obj2 = new ConstructorOverloading();
		ConstructorOverloading obj3 = new ConstructorOverloading(10, 20, 4.5f);
		ConstructorOverloading obj4 = new ConstructorOverloading(7.8f, 10, 20);
	}
}

Sample Output

2. 30
3. 60
1. Constructor with no parameter.
4. 34.5
5. 37.8

When to use

Use this constructor overloading example when learning or revising core Java syntax.

How it works

  1. Execution begins in the main method — the JVM calls this method when you run the class.

  2. A println / print call writes text to the console — part of the sample output below.

  3. A println / print call writes text to the console — part of the sample output below.

  4. A println / print call writes text to the console — part of the sample output below.

  5. A println / print call writes text to the console — part of the sample output below.

  6. A println / print call writes text to the console — part of the sample output below.

  7. ConstructorOverloading obj = new ConstructorOverloading(10, 20); updates a variable used in the calculation or output.

  8. Compare your console output with the sample output for Constructor Overloading to confirm the program behaves correctly.

Best Practices

  • Name classes in PascalCase and follow one public class per file when starting out.
  • Keep main short — delegate work to other methods as programs grow.

Common Mistakes

  • Copying code without understanding each line — practice by changing one statement at a time.
  • Mismatching the public class name and the .java filename.
  • Forgetting semicolons at the end of statements.

Frequently Asked Questions

What does the Constructor Overloading program demonstrate?
It shows how to implement constructor overloading in Java with a complete runnable example and expected console output.
How do I run this Java program?
Save the code in a `.java` file matching the public class name, compile with `javac`, then run with `java ClassName`.
When would I use this pattern?
Use this pattern whenever you need the same logic in homework, practice or small utility tools.

Related Tutorials

Search tutorials