0% found this document useful (0 votes)
4 views3 pages

Function and Operator Overloading Methods

Function overloading in Java allows multiple methods with the same name but different parameters within the same class, enhancing code readability and flexibility. Operator overloading, however, is not supported in Java, meaning operators cannot be redefined for user-defined objects, except for the '+' operator used for string concatenation. The document also outlines key differences between function and operator overloading, including their support and type of polymorphism.

Uploaded by

raahimkhalid777
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Function and Operator Overloading Methods

Function overloading in Java allows multiple methods with the same name but different parameters within the same class, enhancing code readability and flexibility. Operator overloading, however, is not supported in Java, meaning operators cannot be redefined for user-defined objects, except for the '+' operator used for string concatenation. The document also outlines key differences between function and operator overloading, including their support and type of polymorphism.

Uploaded by

raahimkhalid777
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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?

You might also like