Sitemap

What Is Constructor Overloading in Java? (Java Interview QA)

Learn what constructor overloading is in Java, why it’s useful, and how to implement it with real-world examples and simple explanations.

2 min readApr 2, 2025

📌 Definition

Constructor overloading in Java means creating multiple constructors in the same class, each with different parameter lists.

➡️ It allows you to create objects in multiple ways depending on what data is available.

🧱 Basic Syntax

public class Book {
String title;
String author;

// Constructor 1
public Book() {
this.title = "Unknown";
this.author = "Unknown";
}

// Constructor 2
public Book(String title) {
this.title = title;
this.author = "Unknown";
}

// Constructor 3
public Book(String title, String author) {
this.title = title;
this.author = author;
}
}

🧪 Usage:

Book b1 = new Book();                     // uses constructor 1
Book b2 = new Book("Java in Action"); // uses constructor 2
Book b3 = new Book("Spring Boot", "John"); // uses constructor 3

✅ Each constructor initializes the object differently based on the provided parameters.

Why Use Constructor Overloading?

--

--

No responses yet

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy