Q 24 - Constructor Overload #28
Answered
by
HARSHITH-MV
AmjustGettingStarted
asked this question in
Q&A
-
Can a constructor be overloaded |
Beta Was this translation helpful? Give feedback.
Answered by
HARSHITH-MV
Jul 18, 2025
Replies: 1 comment
-
🤓 Yes, a constructor can be overloaded in Java.🔁 Constructor Overloading in JavaConstructor overloading means defining multiple constructors in the same class, each with a different parameter list. ✅ Why Overload Constructors?
🧪 Exampleclass Book {
String title;
int pages;
// Default constructor
Book() {
title = "Unknown";
pages = 0;
}
// Constructor with one parameter
Book(String t) {
title = t;
pages = 100;
}
// Constructor with two parameters
Book(String t, int p) {
title = t;
pages = p;
}
} 🧠 How It Works
|
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
AmjustGettingStarted
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
🤓 Yes, a constructor can be overloaded in Java.
🔁 Constructor Overloading in Java
Constructor overloading means defining multiple constructors in the same class, each with a different parameter list.
✅ Why Overload Constructors?
🧪 Example