오버라이딩(Overriding), 재정의된 멤버 함수의 호출 순서, 오버라이딩 사용 이유

CODEDRAGON Development/Java

반응형


 

오버라이딩(Overriding)

·       오버라이딩은 메서드 재정의라고도 불리며 이는 서로 상속관계로 이루어진 객체들간의 관계에서 비롯됩니다.

·       Inherited methods can be overridden; instance variables cannot be overridden (although they can be redefined in the subclass, but that’s not the same thing, and there’s almost never a need to do it.

 

·       super클래스가 가지고 있는 메서드를 sub클래스에서 똑 같은 것을 새로 만들게 되면 더 이상 super클래스에 있는 이름이 같은 메서드로 호출할 수 없게 됩니다. 이를 오버라이딩 또는 멤버 은폐라고도 합니다.

·       다시 정리하면, 상위 클래스에 정의된 메소드의 이름, 반환형, 매개변수 선언까지 완전히 동일한 메소드하위 클래스에서 다시 정의하는 것으로 하위 클래스에 정의된 메소드에 의해 상위 클래스의 메소드는 가리워집니다.

·       When a method is overridden in a subclass, and that method is invoked on an instance of the subclass, the overridden version of the method is called. (The lowest one wins.)

 

 

 

   


   

 

 

재정의된 멤버 함수의 호출 순서

Phoenix p;

p.sleep(); // Animal sleep() 호출

p.sound(); // Bird sound() 호출

p.move(); // Phoenix move() 호출



   

오버라이딩 사용 이유

재사용성

   

   

반응형