본문 바로가기

Java

오버로딩 및 오버라이딩

오버로딩

오버로딩 은 함수가 하나의 기능만을 구현하는것이 아니라 하나의 메서드 이름으로 여러 기능을 구현하도록 하는 Java의 기능입니다.

즉, 한 클래스 내에 이미 사용하려는 이름과 같은 이름을 가진 메서드가 있더라도, 매개변수의 개수 또는 타입, 순서가 다르면 동일한 이름을 사용해서 메서드를 정의할 수 있습니다.

  • 오버로딩의 조건
    • 메서드의 이름이 같고, 매개변수의 개수, 타입, 순서가 달라야 합니다.
    • '응답 값만' 다른 것은 오버로딩을 할 수 없습니다.
    • 접근 제어자만 다른 것도 오버로딩을 할 수 없습니다.
    • 결론, 오버로딩은 매개변수의 차이로만 구현할 수 있습니다.
  • 오버로딩의 장점
    1. 메서드 이름 하나로 상황에 따른 동작을 개별로 정의할 수 있습니다.
      1. 예를들면 메세지 출력할때 쓰는 println() 이 있습니다.
      2. println() 의 매개변수로는 int, double, String, boolean 등 다양하게 넣을 수 있습니다.
    2. 메서드의 이름을 절약할 수 있습니다.
      1. 만약 오버로딩이 안된다면 println() 는 printlnInt(), printlnDouble() 처럼 메서드명이 길어지고 낭비 되었을 것 입니다.
public class PrintStream extends FilterOutputStream
    implements Appendable, Closeable
{
			...
			
		public void println() {
        newLine();
    }

    public void println(boolean x) {
        if (getClass() == PrintStream.class) {
            writeln(String.valueOf(x));
        } else {
            synchronized (this) {
                print(x);
                newLine();
            }
        }
    }

    public void println(char x) {
        if (getClass() == PrintStream.class) {
            writeln(String.valueOf(x));
        } else {
            synchronized (this) {
                print(x);
                newLine();
            }
        }
    }

    public void println(int x) {
        if (getClass() == PrintStream.class) {
            writeln(String.valueOf(x));
        } else {
            synchronized (this) {
                print(x);
                newLine();
            }
        }
    }

    public void println(long x) {
        if (getClass() == PrintStream.class) {
            writeln(String.valueOf(x));
        } else {
            synchronized (this) {
                print(x);
                newLine();
            }
        }
    }

    public void println(float x) {
        if (getClass() == PrintStream.class) {
            writeln(String.valueOf(x));
        } else {
            synchronized (this) {
                print(x);
                newLine();
            }
        }
    }

    public void println(double x) {
        if (getClass() == PrintStream.class) {
            writeln(String.valueOf(x));
        } else {
            synchronized (this) {
                print(x);
                newLine();
            }
        }
    }

    public void println(char[] x) {
        if (getClass() == PrintStream.class) {
            writeln(x);
        } else {
            synchronized (this) {
                print(x);
                newLine();
            }
        }
    }

    public void println(String x) {
        if (getClass() == PrintStream.class) {
            writeln(String.valueOf(x));
        } else {
            synchronized (this) {
                print(x);
                newLine();
            }
        }
    }

    public void println(Object x) {
        String s = String.valueOf(x);
        if (getClass() == PrintStream.class) {
            // need to apply String.valueOf again since first invocation
            // might return null
            writeln(String.valueOf(s));
        } else {
            synchronized (this) {
                print(s);
                newLine();
            }
        }
    }


		  ...
}

 

 

 

 

'Java' 카테고리의 다른 글

접근제어자  (0) 2023.04.05
기본형 & 참조형 매개변수  (0) 2023.04.04
컬렉션 및 예제  (0) 2023.04.03
2차원 배열과 3차원 배열  (0) 2023.04.03
참조 변수의 얕은 복사, 깊은 복사 + String 메소드  (0) 2023.04.03