Skip to content

Commit 2b0b270

Browse files
authored
Merge pull request #715 from psychomita/main
Added Method Overloading page
2 parents c41070e + b8c277c commit 2b0b270

File tree

2 files changed

+118
-1
lines changed

2 files changed

+118
-1
lines changed

src/navs/documentation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ export const documentationNav = {
2828
pages['continue-statement'],
2929
],
3030
'Java Arrays': [pages['arrays'], pages['multidimensional-arrays']],
31-
'Java OOPS(I)': [pages['class-objects'], pages['methods'], pages['static-keyword']],
31+
'Java OOPS(I)': [pages['class-objects'], pages['methods'], pages['method-overloading'], pages['static-keyword']],
3232
}

src/pages/docs/method-overloading.mdx

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
---
2+
title: Java Method Overloading
3+
description: In this tutorial, we will learn how to implements methods overloading or function overloading in Java.
4+
---
5+
6+
## Java Method Overloading
7+
In Java, method overloading allows multiple methods with the same name but different parameter lists to coexist in the same class. This can involve variations in the number or types of parameters, or both. Overloaded methods provide flexibility by offering different ways to call a method, depending on the parameters passed.
8+
9+
### Example:
10+
11+
```java
12+
void func() {}
13+
void func(int a) {}
14+
float func(double a) {}
15+
float func(int a, float b) {}
16+
```
17+
In this example, the `func()` method is overloaded with different parameter configurations. While the return types differ, this does not affect method overloading; overloading is purely based on parameters.
18+
19+
## Why Use Method Overloading?
20+
Consider the scenario where you need to sum numbers but could have different parameter requirements (e.g., 2 or 3 numbers). You could create separate methods like `sum2(int, int)` and `sum3(int, int, int)`. However, using method overloading allows a more readable approach by keeping the method name the same:
21+
22+
```java
23+
int sum(int a, int b) { ... }
24+
int sum(int a, int b, int c) { ... }
25+
```
26+
Here, `sum()` is overloaded to handle different numbers of arguments.
27+
28+
## Performing Method Overloading in Java
29+
Method overloading can be achieved in two main ways:
30+
31+
### 1. Changing the Number of Parameters
32+
33+
```java
34+
class MethodOverloading {
35+
private static void display(int a) {
36+
System.out.println("Argument: " + a);
37+
}
38+
39+
private static void display(int a, int b) {
40+
System.out.println("Arguments: " + a + " and " + b);
41+
}
42+
43+
public static void main(String[] args) {
44+
display(1);
45+
display(1, 4);
46+
}
47+
}
48+
```
49+
### Output
50+
51+
```bash
52+
Argument: 1
53+
Arguments: 1 and 4
54+
```
55+
56+
### 2. Changing the Parameter Data Type
57+
58+
```java
59+
class MethodOverloading {
60+
private static void display(int a) {
61+
System.out.println("Got Integer data.");
62+
}
63+
64+
private static void display(String a) {
65+
System.out.println("Got String object.");
66+
}
67+
68+
public static void main(String[] args) {
69+
display(1);
70+
display("Hello");
71+
}
72+
}
73+
```
74+
### Output
75+
76+
```bash
77+
Got Integer data.
78+
Got String object.
79+
```
80+
## Real-World Example
81+
In a utility class, you might use overloading to format numbers:
82+
83+
```java
84+
class HelperService {
85+
private String formatNumber(int value) {
86+
return String.format("%d", value);
87+
}
88+
89+
private String formatNumber(double value) {
90+
return String.format("%.3f", value);
91+
}
92+
93+
private String formatNumber(String value) {
94+
return String.format("%.2f", Double.parseDouble(value));
95+
}
96+
97+
public static void main(String[] args) {
98+
HelperService hs = new HelperService();
99+
System.out.println(hs.formatNumber(500));
100+
System.out.println(hs.formatNumber(89.9934));
101+
System.out.println(hs.formatNumber("550"));
102+
}
103+
}
104+
```
105+
### Output
106+
```bash
107+
500
108+
89.993
109+
550.00
110+
```
111+
112+
## Important Points
113+
- Method overloading requires methods with different parameters within the same class.
114+
- Overloading is achieved by varying either the number or type of parameters.
115+
- Changing only the return type does not constitute overloading; there must be a parameter difference.
116+
117+
**Tip:** *Constructor overloading in Java works similarly to method overloading.*

0 commit comments

Comments
 (0)
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