1️⃣ Function Overloading in Java
🔹 Definition:
Function Overloading means defining multiple methods with the same
name but different parameters in the same class.
👉 The difference must be in:
Number of parameters
Type of parameters
Order of parameters
🔹 Example:
class Calculator {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}
✔ Same method name: add()
✔ Different parameter lists
✔ This is compile-time polymorphism
🔹 Why We Use Function Overloading?
Improves code readability
Same function name for similar tasks
Increases flexibility
Supports polymorphism
✅ 2️⃣ Operator Overloading in Java
🔹 Definition:
Operator Overloading means giving special meaning to operators for user-
defined objects.
❗ Important:
👉 Java does NOT support operator overloading like C++.
You cannot redefine operators such as +, -, *, etc., for your own classes.
🔹 Example (Not Allowed in Java ❌)
class Test {
int x;
// This is NOT allowed in Java
// public Test operator+(Test t) { }
}
🔹 What Java Allows
Java only allows operator overloading internally for:
✔ + operator for String concatenation
Example:
class Test {
public static void main(String[] args) {
String a = "Hello ";
String b = "World";
[Link](a + b); // Allowed
}
}
Here + works for strings.
✅ Key Difference
Function Operator
Feature
Overloading Overloading
Supported in ❌ No (except String
✅ Yes
Java +)
Type Method level Operator level
Polymorphism
Compile-time Not supported
Type
✅ 5 Theory Questions (One Line)
1. What is function overloading in Java?
2. What conditions must be satisfied for method overloading?
3. Why is function overloading called compile-time polymorphism?
4. Does Java support operator overloading? Explain.
5. Why does Java restrict operator overloading?