Skip to content

Latest commit

 

History

History
171 lines (157 loc) · 5.84 KB

File metadata and controls

171 lines (157 loc) · 5.84 KB

다형성

  • 추상화와 상속등과 같은 OOP(Object Oriented Programming)의 기본이 되는 특징 중에 하나로,
    다수를 의미하는 “poly”와 형태를 의미하는 “morphs”에서 파생되었다.
  • 하나의 객체가 여러 가지 타입을 가질 수 있는 것을 의미한다.
    (하나의 메소드와 클래스가 존재할 때 이것들이 다양한 방법으로 동작할 수 있음을 의미한다. )

오버로딩(Overloading)

  • 하나의 클래스 내에 동일한 이름을 갖는 메소드가 여러개 정의되어있는 경우, 매개 변수의 개수, 형식 및 순서와 Return값이 다른 경우 다른 메소드로 인식하는 것
  • 오버로딩의 조건
    • 메소드의 이름이 같아야 한다.
      (ex) overloadingTest()
    • 메소드의 매개 변수(Parameter)의 개수 혹은 타입이 달라야한다.
    • 매개 변수가 같고, 리턴 타입이 다른 경우에는 오버로딩이 성립하지 않는다.
      int add(int a, int b) { return a + b }
      int add(int a, int b) { return a * b }
      
  • Example
class OverloadingExample {
   public static void overloadingTest()   {
        System.out.println("Call by overloadingTest no Parameters");
    }

    public static void overloadingTest(int a)   {
        System.out.println("Call by overloadingTest with Parameter");
        System.out.println("Parameter is " + a);
    }

    public static void overloadingTest(int a, String strParam)   {
        System.out.println("Call by overloadingTest no Parameters");
                System.out.println("Parameter is " + a + " and " + strParam);
    }

    public static void main(String[] args)  {
        overloadingTest();
        overloadingTest(10);
        overloadingTest(100, "Hello World!");
    }
}

오버라이딩(Overriding)

  • 부모 클래스(슈퍼 클래스)를 상속하는 경우, 자식 클래스(하위 클래스)에 상위 클래스와 동일한 이름, 매개변수 및 반환 유형을 가진 메소드를 재정의한다.
  • 오버라이딩의 조건
    • 상속받는 클래스(자식 클래스)와 상속하는 클래스(부모 클래스) 사이에서 성립한다.
    • 메소드의 이름과 매개변수, 반환 유형이 동일해야 한다.
    • static으로 정의된 메소드는 클래스에 속하는 메소드이기 때문에 상속되지 않는다.
    • 접근 제어자는 부모 클래스의 접근 제어자보다 좁은 범위로 변경할 수 없다.
      class Parents {
        protected void printHello() {
          System.out.println("Hello world!")
        }
      }
      class Child {
        public void printHello() {    // protected에서 public으로 오버라이딩 될 수 없다.
          System.out.println("Hello world!")
        }
      }
      
    • final 예약어가 지정된 메소드는 오버라이딩 되지 않는다.
    • 자식 클래스가 부모 클래스의 메소드보다 많은 수의 Exception을 선언할 수 없다.
      class Parents {
        public void printHello() throws IOException{
          // ~~~
        }
      }
      class Child {
         public void printHello() throws IOException, SQLException {    // 자식 클래스가 부모 클래스의 메소드보다 많은 수의 Exception을 선언할 수 없다.
          // ~~~
        }
      } 
      ------------------------------------------------------------------------------------------------------------------------------------------
      class Parents {
        public void printHello() throws IOException, SQLException{
          // ~~~
        }
      }
      class Child {
         public void printHello() throws Exception {    // 위의 경우 와는 다르게 개수는 더 적어보이지만 Exception이 IOException, SQLException을 포함하는 최상위 Exception이기 때문에 에러가 발생한다.
          // ~~~
        }
      } 
      
  • Example
class Parents   {
    public void hello() {
        System.out.println("This method is called by Parents Class");
    }
}

class ChildOne extends Parents{
    public void hello() {
        System.out.println("This method is called by ChildOne Class");
    }
}

class ChildTwo extends Parents{
    public void hello() {
        System.out.println("This method is called by ChildTwo Class");
    }
}

public class OverroadingTest {
    public static void main(String[] args) {
        Parents parents = new Parents();
        ChildOne childOne = new ChildOne();
        ChildTwo childTwo = new ChildTwo();

        parents.hello();
        childOne.hello();
        childTwo.hello();
    }
}

※ 오버로딩의 경우 대상이 되는 함수를 컴파일 시점에 지정하고, 오버라이딩의 경우 런타임 시점에 정의한다.

  • Compile-Time Polymorphism(=Static Polymorphism)
    • 메소드에 대한 호출이 컴파일 타임에 이루어진다.
    • 메소드 오버로딩(Method Overloading)이 이에 해당한다.
    • 메소드를 오버로딩할 때는 하나의 이름으로 선언하지만 컴파일 과정에서 다른 이름의 메소드로 재정의된다.
  • Run-Time Polymorphism(=Dynamic Binding =Dynamic Method Dispatch)
    • 매소드에 대한 호출이 런타임에 동적으로 이루어진다.
    • 메소드 오버라이딩(Method OverRiding)이 이에 해당한다.

업캐스팅(Upcasting)

  • 자식 객체를 부모 객체로 TypeCasting하는 것을 의미한다.
  • Example
class Parents {
  public void printHello(){
     // ~~~
  }
}
class Child {
   public void printHello(){
    // ~~~
  }
} 

public class UpcastingExample() {
  Parents parents = new Child();
}

다운 캐스팅(Downcasting)

  • 부모 객체를 강제로 자식 객체로 TypeCasting하는 것을 의미한다.
  • Example
class Parents {
  public void printHello(){
     // ~~~
  }
}
class Child {
   public void printHello(){
    // ~~~
  }
} 

public class UpcastingExample() {
  Parents parents = new Child();
  Child c = (Child) p;
}