8000 Added Method Overloading page by psychomita · Pull Request #715 · javaistic/javaistic · GitHub
[go: up one dir, main page]

Skip to content

Added Method Overloading page #715

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 26, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
8000
Diff view
Diff view
Prev Previous commit
Added method overloading page
  • Loading branch information
psychomita committed Oct 26, 2024
commit b8c277c72279c5c9e9f7cdecf927d23427760c6f
117 changes: 117 additions & 0 deletions src/pages/docs/method-overloading.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
---
title: Java Method Overloading
description: In this tutorial, we will learn how to implements methods overloading or function overloading in Java.
---

## Java Method Overloading
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.

### Example:

```java
void func() {}
void func(int a) {}
float func(double a) {}
float func(int a, float b) {}
```
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.

## Why Use Method Overloading?
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:

```java
int sum(int a, int b) { ... }
int sum(int a, int b, int c) { ... }
```
Here, `sum()` is overloaded to handle different numbers of arguments.

## Performing Method Overloading in Java
Method overloading can be achieved in two main ways:

### 1. Changing the Number of Parameters

```java
class MethodOverloading {
private static void display(int a) {
System.out.println("Argument: " + a);
}

private static void display(int a, int b) {
System.out.println("Arguments: " + a + " and " + b);
}

public static void main(String[] args) {
display(1);
display(1, 4);
}
}
```
### Output

```bash
Argument: 1
Arguments: 1 and 4
```

### 2. Changing the Parameter Data Type

```java
class MethodOverloading {
private static void display(int a) {
System.out.println("Got Integer data.");
}

private static void display(String a) {
System.out.println("Got String object.");
}

public static void main(String[] args) {
display(1);
display("Hello");
}
}
```
### Output

```bash
Got Integer data.
Got String object.
```
## Real-World Example
In a utility class, you might use overloading to format numbers:

```java
class HelperService {
private String formatNumber(int value) {
return String.format("%d", value);
}

private String formatNumber(double value) {
return String.format("%.3f", value);
}

private String formatNumber(String value) {
return String.format("%.2f", Double.parseDouble(value));
}

public static void main(String[] args) {
HelperService hs = new HelperService();
System.out.println(hs.formatNumber(500));
System.out.println(hs.formatNumber(89.9934));
System.out.println(hs.formatNumber("550"));
}
}
```
### Output
```bash
500
89.993
550.00
```

## Important Points
- Method overloading requires methods with different parameters within the same class.
- Overloading is achieved by varying either the number or type of parameters.
- Changing only the return type does not constitute overloading; there must be a parameter difference.

**Tip:** *Constructor overloading in Java works similarly to method overloading.*
Loading
0