> For the complete documentation index, see [llms.txt](https://nbcamp.gitbook.io/java-handbook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://nbcamp.gitbook.io/java-handbook/part-05./undefined-6.md).

# 오버로딩

> ## 한 클래스 안에 같은 이름의 메서드를 여러 개 정의하는 것입니다.
>
> ### 오버로딩의 성립 조건
>
> 1. 메서드 이름이 같아야 합니다.
> 2. 매개변수의 개수 또는 타입, 위치가 달라야 합니다.
> 3. 반환타입에는 영향을 받지 않습니다.&#x20;
>
> ### 사용을 하는 목적
>
> * 매개변수 즉, <mark style="color:blue;">입력하는 값이 다르지만 같은 기능을 수행하는 경우</mark>가 많을 때 사용성 및 효율을 높이기 위해 오버로딩을 사용합니다.

* 아래 예제코드로 학습해 보겠습니다.

{% code lineNumbers="true" %}

```java
class Overloading8_1 {

    int add(int a, int b) {
        System.out.println("int add(int a, int b)");
        return a + b;
    }

//    void add(int a, int b) { // 반환 타입이 다르다고 오버로딩이 성립되지는 않음
//        System.out.println("void add(int a, int b)");
//        System.out.println("a + b = " + a + b);
//    }

    long add(long a, long b) {
        System.out.println("long add(long a, long b)");
        return a + b;
    }

    long add(long a, int b) {
        System.out.println("long add(long a, int b)");
        return a + b;
    }

    long add(int a, long b) {
        System.out.println("long add(int a, long b)");
        return a + b;
    }

}

class Overloading8_1Main {
    public static void main(String[] args) {
        Overloading8_1 test = new Overloading8_1();
        System.out.println(test.add(10, 20));
        System.out.println(test.add(13L, 17L));
        System.out.println(test.add(5L, 10));
        System.out.println(test.add(12, 23L));

        System.out.println();

        // 그런데 이때 위에 int add(int a, int b)  를 주석한다면
        test.add(10, 20); // 여기에 Ambiguous method call Error 가 발생합니다.
        // int, int 는 (long, int) , (int, long) 2개의 메서드 모두 가능하기 때문에 컴퓨터가 하나를
        // 마음대로 선택할 수가 없어서 발생하는 오류입니다.

    }
}
```

{% endcode %}

{% hint style="info" %}
Ref. Java의 정석 기초편 Chapter6(30, 31)

Ref. [오버로딩](https://www.youtube.com/watch?v=2rDeHSO4bdw\&list=PLW2UjW795-f6xWA2_MUhEVgPauhGl3xIp\&index=66)
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://nbcamp.gitbook.io/java-handbook/part-05./undefined-6.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
