0% found this document useful (0 votes)
2 views12 pages

Method Overloading in Java

Method overloading in Java allows multiple methods with the same name but different parameters in the same class, enabling compile-time polymorphism. It improves code readability and reduces naming complexity, while methods can be overloaded by changing the number, type, or order of parameters. Important rules include that return type alone cannot overload methods, and ambiguity can arise if the compiler cannot determine which method to call.

Uploaded by

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

Method Overloading in Java

Method overloading in Java allows multiple methods with the same name but different parameters in the same class, enabling compile-time polymorphism. It improves code readability and reduces naming complexity, while methods can be overloaded by changing the number, type, or order of parameters. Important rules include that return type alone cannot overload methods, and ambiguity can arise if the compiler cannot determine which method to call.

Uploaded by

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

✨ Method Overloading in Java

1️⃣ What is Method Overloading? 🔁


Method Overloading means having multiple methods with the same name in the same
class, but with different parameters.

Java decides which method to call based on the method signature.

🧠 Method Signature
Method Name + Parameter List

Return type ❌ not included

💻 Example
class Calculator {

int add(int a, int b) {


return a + b;
}

int add(int a, int b, int c) {


return a + b + c;
}

double add(double a, double b) {


return a + b;
}
}

🚀 Usage
public class Main {
public static void main(String[] args) {

Calculator c = new Calculator();

Soumik Karmakar - Object Oriented Programming with Java


[Link]([Link](2,3)); // 5
[Link]([Link](2,3,4)); // 9
[Link]([Link](2.5,3.1)); // 5.6
}
}

2️⃣ Why Method Overloading is Used 🎯


Benefits

📖
🔄​
✔ Improves code readability ​

⚙️
✔ Enables same method name for similar operations

🧹
✔ Implements Compile-Time Polymorphism ​
✔ Reduces method naming complexity

❌ Without Overloading
addInt()
addDouble()
addThreeNumbers()

✅ With Overloading
add()

Cleaner and easier ✨

3️⃣ Ways to Achieve Method Overloading 🛠


3.1️⃣ Change Number of Parameters
class Demo {
🔢
void show(int a){
[Link](a);

Soumik Karmakar - Object Oriented Programming with Java


}

void show(int a, int b){


[Link](a + " " + b);
}
}

3.2️⃣ Change Type of Parameters


class Demo {
🔤
void print(int a){
[Link]("Integer");
}

void print(double a){


[Link]("Double");
}
}

3.3️⃣ Change Order of Parameters


class Demo {
🔄
void display(int a, String b){
[Link]("int then String");
}

void display(String b, int a){


[Link]("String then int");
}
}

4️⃣ Important Rule 🚨


Return Type Alone Cannot Overload

Soumik Karmakar - Object Oriented Programming with Java


❌ Invalid
class Demo {

int sum(int a, int b){


return a + b;
}

double sum(int a, int b){


return a + b;
}
}

❗ Compiler Error
method sum(int,int) is already defined

Because the method signature is same.

⬆️
5️⃣ Method Overloading with Type
Promotion
Java automatically promotes smaller types.

🔄 Promotion Hierarchy
byte → short → int → long → float → double

Example
class Test {

void show(int a){


[Link]("int");
}

void show(double a){


[Link]("double");
}

public static void main(String[] args) {

Soumik Karmakar - Object Oriented Programming with Java


Test t = new Test();
[Link](5);
}
}

Output
int

Because exact match exists.

Example 2
class Test {

void show(double a){


[Link]("double");
}

public static void main(String[] args) {


Test t = new Test();
[Link](5);
}
}

Output
double

Because int → double promotion.

6️⃣ Ambiguity Problem ⚠️


Sometimes Java cannot decide which method to call.

Example
class Test {

void show(int a, double b){


[Link]("method1");
}

Soumik Karmakar - Object Oriented Programming with Java


void show(double a, int b){
[Link]("method2");
}

public static void main(String[] args) {


Test t = new Test();
[Link](10,10);
}
}

❌ Error
reference to show is ambiguous

Because both methods match equally.

7️⃣ Method Overloading with Varargs 📦


Varargs allow variable number of arguments.

class Demo {

void show(int a){


[Link]("Single Argument");
}

void show(int... a){


[Link]("Varargs Method");
}
}

Example
public class Main {

public static void main(String[] args) {

Demo d = new Demo();

[Link](10);
}

Soumik Karmakar - Object Oriented Programming with Java


}

Output
Single Argument

Because exact match > varargs.

8️⃣ Overloading vs Overriding ⚔️


Feature Method Overloading Method Overriding

Class Same Class Parent & Child

Parameters Must be different Must be same

Return Type Can differ Must be compatible

Binding Compile Time Runtime

Polymorphism Compile-time Runtime

9️⃣ Constructor Overloading 🏗


Constructors can also be overloaded.

class Student {

String name;
int age;

Student(){
[Link]("Default Constructor");
}

Student(String n){
name = n;
}

Student(String n, int a){

Soumik Karmakar - Object Oriented Programming with Java


name = n;
age = a;
}
}

🔟 Real Life Example 💡


Example: Print Method

class Printer {

void print(int x){


[Link](x);
}

void print(String s){


[Link](s);
}

void print(double d){


[Link](d);
}
}

🧠 MCQs to Test Your Concepts


❓ MCQ 1
Which concept does method overloading implement?

A) Runtime Polymorphism​
B) Compile-time Polymorphism​
C) Inheritance​
D) Abstraction

✅ Answer: B

Soumik Karmakar - Object Oriented Programming with Java


❓ MCQ 2
Which of the following is not allowed for overloading?

A) Changing parameter type​


B) Changing parameter number​
C) Changing parameter order​
D) Changing return type only

✅ Answer: D

❓ MCQ 3
Method signature includes:

A) Method name + return type​


B) Method name + parameters​
C) Method name + modifiers​
D) Return type + parameters

✅ Answer: B

❓ MCQ 4
Output?

class Test{
void show(int a){
[Link]("int");
}

void show(double a){


[Link]("double");
}

public static void main(String[] args){


Test t = new Test();
[Link](10);
}
}

Soumik Karmakar - Object Oriented Programming with Java


A) double​
B) int​
C) error​
D) none

✅ Answer: B

❓ MCQ 5
Which feature allows multiple methods with same name?

A) Encapsulation​
B) Overloading​
C) Overriding​
D) Inheritance

✅ Answer: B

❓ MCQ 6
Which is correct?

A) Overloading happens at runtime​


B) Overloading happens at compile time​
C) Overloading requires inheritance​
D) Overloading requires interfaces

✅ Answer: B

❓ MCQ 7
Which of these can be overloaded?

A) Methods​
B) Constructors​
C) Operators​
D) Both A and B

✅ Answer: D

Soumik Karmakar - Object Oriented Programming with Java


❓ MCQ 8
Output?

class Demo{
void show(int a){
[Link]("A");
}

void show(int... a){


[Link]("B");
}

public static void main(String[] args){


Demo d = new Demo();
[Link](5);
}
}

A) A​
B) B​
C) Error​
D) None

✅ Answer: A

❓ MCQ 9
Which promotion happens?

byte → ?

A) double​
B) short​
C) char​
D) boolean

✅ Answer: B

❓ MCQ 10
Soumik Karmakar - Object Oriented Programming with Java
Which will cause ambiguity error?

A)

show(int,double)
show(double,int)

B)

show(int)
show(double)

C)

show(int)
show(int,int)

D)

show(String)
show(Object)

✅ Answer: A

🎯 Final One-Line Definition


✨ Method Overloading = Same method name + different parameters (compile-time
polymorphism).

Soumik Karmakar - Object Oriented Programming with Java

You might also like