023_메소드 오버로딩

Dec 20, 2023
023_메소드 오버로딩

메소드 오버로딩(Method Overloading)

  • 동일한 이름의 메소드를 여러 개 정의하는 것을 의미한다.
  • 오버로딩은 우리말로 ‘중복정의’이다.
  • 메소드 오버로딩은 다형성을 구현하는 한 가지 방법이다.
메소드 오버로딩은 왜 유용할까? 이유는? 데이터의 개수가 다르지만 기능이 비슷한 경우 서로 다른 이름을 사용하는 번거로움을 덜어준다. 쉽게 말해 같은 이름을 중복해 여러 번 사용 할 수 있다.
메소드 오버로딩 예시 코드
package ex04; public class MyMath { int add(int x, int y) { return x + y; } int add(int x, int y, int z) { return x + y + z; } int add(int x, int y, int z, int w) { return x + y + z + w; } public static void main(String[] args) { MyMath obj = new MyMath(); System.out.println(obj.add(10, 20)); System.out.println(obj.add(10, 20, 30)); System.out.println(obj.add(10, 20, 30, 40)); } }
출력 결과
notion image
Share article
RSSPowered by inblog