Java에서 재정의는 하위 클래스나 하위 클래스가 해당 상위 클래스나 상위 클래스 중 하나에서 이미 제공한 메서드의 특정 구현을 제공할 수 있도록 하는 기능입니다. 하위 클래스의 메서드가 상위 클래스의 메서드와 동일한 이름, 동일한 매개변수 또는 서명, 동일한 반환 유형(또는 하위 유형)을 갖는 경우 하위 클래스의 메서드는 다음과 같습니다. 우세하다 슈퍼클래스의 메소드.
메소드 재정의는 Java가 달성하는 방법 중 하나입니다. 런타임 다형성 . 실행되는 메서드의 버전은 해당 메서드를 호출하는 데 사용되는 개체에 따라 결정됩니다. 상위 클래스의 객체를 사용하여 메서드를 호출하는 경우 상위 클래스의 버전이 실행되지만, 하위 클래스의 객체를 사용하여 메서드를 호출하는 경우 하위 클래스의 버전이 실행됩니다. 다시 말해서, 참조되는 객체의 유형입니다. (참조 변수의 유형이 아님) 실행될 재정의된 메서드의 버전을 결정합니다.
Java의 메소드 재정의 예
다음은 Java 메소드 재정의의 구현입니다.
자바
// Java program to demonstrate> // method overriding in java> // Base Class> class> Parent {> >void> show() { System.out.println(>'Parent's show()'>); }> }> // Inherited class> class> Child>extends> Parent {> >// This method overrides show() of Parent> >@Override> void> show()> >{> >System.out.println(>'Child's show()'>);> >}> }> // Driver class> class> Main {> >public> static> void> main(String[] args)> >{> >// If a Parent type reference refers> >// to a Parent object, then Parent's> >// show is called> >Parent obj1 =>new> Parent();> >obj1.show();> >// If a Parent type reference refers> >// to a Child object Child's show()> >// is called. This is called RUN TIME> >// POLYMORPHISM.> >Parent obj2 =>new> Child();> >obj2.show();> >}> }> |
>
>산출
Parent's show() Child's show()>
Java 메소드 재정의 규칙
1. 재정의 및 액세스 수정자
그만큼 접근 수정자 재정의 메서드는 재정의된 메서드보다 더 많은 액세스를 허용할 수 있지만 그보다 더 적은 액세스를 허용할 수는 없습니다. 예를 들어, 슈퍼클래스의 보호된 인스턴스 메서드는 하위 클래스에서 공개로 설정할 수 있지만 비공개로 설정할 수는 없습니다. 그렇게 하면 컴파일 타임 오류가 발생합니다.
자바
// A Simple Java program to demonstrate> // Overriding and Access-Modifiers> class> Parent {> >// private methods are not overridden> >private> void> m1()> >{> >System.out.println(>'From parent m1()'>);> >}> >protected> void> m2()> >{> >System.out.println(>'From parent m2()'>);> >}> }> class> Child>extends> Parent {> >// new m1() method> >// unique to Child class> >private> void> m1()> >{> >System.out.println(>'From child m1()'>);> >}> >// overriding method> >// with more accessibility> >@Override> public> void> m2()> >{> >System.out.println(>'From child m2()'>);> >}> }> // Driver class> class> Main {> >public> static> void> main(String[] args)> >{> >Parent obj1 =>new> Parent();> >obj1.m2();> >Parent obj2 =>new> Child();> >obj2.m2();> >}> }> |
>
>산출
From parent m2() From child m2()>
2. 최종 메서드는 재정의될 수 없습니다.
메서드를 재정의하지 않으려면 다음과 같이 선언합니다. 결정적인 . 참조하세요 상속과 함께 Final 사용 .
자바
// A Java program to demonstrate that> // final methods cannot be overridden> class> Parent {> >// Can't be overridden> >final> void> show() {}> }> class> Child>extends> Parent {> >// This would produce error> >void> show() {}> }> |
>
>
산출
13: error: show() in Child cannot override show() in Parent void show() { } ^ overridden method is final> 3. 정적 메서드는 재정의할 수 없습니다(메소드 재정의 vs 메서드 숨김):
기본 클래스의 정적 메서드와 동일한 시그니처를 사용하여 정적 메서드를 정의하는 것을 다음과 같이 부릅니다. 메소드 숨기기 . 다음 표에는 슈퍼 클래스의 메서드와 동일한 시그니처를 사용하여 메서드를 정의할 때 발생하는 일이 요약되어 있습니다.
| 슈퍼클래스 인스턴스 방법 | 슈퍼클래스 정적 메서드 | |
|---|---|---|
| 서브클래스 인스턴스 방법 | 재정의 | 컴파일 시간 오류를 생성합니다. |
| 서브클래스 정적 메서드 | 컴파일 시간 오류를 생성합니다. | 숨기기 |
자바
알파벳에 번호를 매기다
// Java program to show that> // if the static method is redefined by> // a derived class, then it is not> // overriding, it is hiding> class> Parent {> >// Static method in base class> >// which will be hidden in subclass> >static> void> m1()> >{> >System.out.println(>'From parent '> >+>'static m1()'>);> >}> >// Non-static method which will> >// be overridden in derived class> >void> m2()> >{> >System.out.println(> >'From parent '> >+>'non - static(instance) m2() '>);> >}> }> class> Child>extends> Parent {> >// This method hides m1() in Parent> >static> void> m1()> >{> >System.out.println(>'From child static m1()'>);> >}> >// This method overrides m2() in Parent> >@Override> public> void> m2()> >{> >System.out.println(> >'From child '> >+>'non - static(instance) m2() '>);> >}> }> // Driver class> class> Main {> >public> static> void> main(String[] args)> >{> >Parent obj1 =>new> Child();> >// As per overriding rules this> >// should call to class Child static> >// overridden method. Since static> >// method can not be overridden, it> >// calls Parent's m1()> >obj1.m1();> >// Here overriding works> >// and Child's m2() is called> >obj1.m2();> >}> }> |
>
>산출
From parent static m1() From child non - static(instance) m2()>
4. 프라이빗 메서드는 재정의될 수 없습니다.
비공개 메소드 컴파일 시간 동안 결합되므로 재정의할 수 없습니다. 따라서 하위 클래스에서는 개인 메서드를 재정의할 수도 없습니다.(참조 이것 자세한 내용은).
자바
class> SuperClass {> >private> void> privateMethod()> >{> >System.out.println(> >'This is a private method in SuperClass'>);> >}> >public> void> publicMethod()> >{> >System.out.println(> >'This is a public method in SuperClass'>);> >privateMethod();> >}> }> class> SubClass>extends> SuperClass {> >// This is a new method with the same name as the> >// private method in SuperClass> >private> void> privateMethod()> >{> >System.out.println(> >'This is a private method in SubClass'>);> >}> >// This method overrides the public method in SuperClass> >public> void> publicMethod()> >{> >System.out.println(> >'This is a public method in SubClass'>);> >privateMethod();>// calls the private method in> >// SubClass, not SuperClass> >}> }> public> class> Test {> >public> static> void> main(String[] args)> >{> >SuperClass obj1 =>new> SuperClass();> >obj1.publicMethod();>// calls the public method in> >// SuperClass> >SubClass obj2 =>new> SubClass();> >obj2.publicMethod();>// calls the overridden public> >// method in SubClass> >}> }> |
>
>산출
This is a public method in SuperClass This is a private method in SuperClass This is a public method in SubClass This is a private method in SubClass>
5. 재정의 메서드는 동일한 반환 유형(또는 하위 유형)을 가져야 합니다.
Java 5.0부터는 자식 클래스의 재정의 메서드에 대해 다른 반환 유형을 가질 수 있지만 자식의 반환 유형은 부모 반환 유형의 하위 유형이어야 합니다. 이 현상은 다음과 같이 알려져 있습니다. 공변 반환 유형 .
자바
class> SuperClass {> >public> Object method()> >{> >System.out.println(> >'This is the method in SuperClass'>);> >return> new> Object();> >}> }> class> SubClass>extends> SuperClass {> >public> String method()> >{> >System.out.println(> >'This is the method in SubClass'>);> >return> 'Hello, World!'>;> >}> }> public> class> Test {> >public> static> void> main(String[] args)> >{> >SuperClass obj1 =>new> SuperClass();> >obj1.method();> >SubClass obj2 =>new> SubClass();> >obj2.method();> >}> }> |
>
>산출
This is the method in SuperClass This is the method in SubClass>
6. 하위 클래스에서 재정의된 메서드 호출
다음을 사용하여 재정의 메서드에서 부모 클래스 메서드를 호출할 수 있습니다. 슈퍼 키워드 .
자바
// A Java program to demonstrate that overridden> // method can be called from sub-class> // Base Class> class> Parent {> >void> show() { System.out.println(>'Parent's show()'>); }> }> // Inherited class> class> Child>extends> Parent {> >// This method overrides show() of Parent> >@Override> void> show()> >{> >super>.show();> >System.out.println(>'Child's show()'>);> >}> }> // Driver class> class> Main {> >public> static> void> main(String[] args)> >{> >Parent obj =>new> Child();> >obj.show();> >}> }> |
>
>산출
Parent's show() Child's show()>
재정의 및 생성자
부모 클래스와 자식 클래스는 동일한 이름을 가진 생성자를 가질 수 없으므로 생성자를 재정의할 수 없습니다(생성자 이름은 항상 클래스 이름과 동일해야 합니다).
재정의 및 예외 처리
다음은 예외 처리와 관련된 메서드를 재정의할 때 주의해야 할 두 가지 규칙입니다.
규칙 #1
상위 클래스 재정의 메서드가 예외를 발생시키지 않는 경우 하위 클래스 재정의 메서드는 예외만 발생시킬 수 있습니다. 확인되지 않은 예외 , 확인된 예외가 발생하면 컴파일 시간 오류가 발생합니다.
자바
dfs 알고리즘
// Java program to demonstrate overriding when> // superclass method does not declare an exception> class> Parent {> >void> m1() { System.out.println(>'From parent m1()'>); }> >void> m2() { System.out.println(>'From parent m2()'>); }> }> class> Child>extends> Parent {> >@Override> >// no issue while throwing unchecked exception> >void> m1()>throws> ArithmeticException> >{> >System.out.println(>'From child m1()'>);> >}> >@Override> >// compile-time error> >// issue while throwing checked exception> >void> m2()>throws> Exception> >{> >System.out.println(>'From child m2'>);> >}> }> |
>
>
산출
error: m2() in Child cannot override m2() in Parent void m2() throws Exception{ System.out.println('From child m2');} ^ overridden method does not throw Exception> 규칙 #2
상위 클래스 재정의 메서드가 예외를 발생시키는 경우 하위 클래스 재정의 메서드는 동일한 하위 클래스 예외만 발생시킬 수 있습니다. 부모 예외 발생 예외 계층 컴파일 시간 오류가 발생합니다. 또한 하위 클래스 재정의 메서드가 예외를 발생시키지 않는 경우에도 문제가 없습니다.
자바
// Java program to demonstrate overriding when> // superclass method does declare an exception> class> Parent {> >void> m1()>throws> RuntimeException> >{> >System.out.println(>'From parent m1()'>);> >}> }> class> Child1>extends> Parent {> >@Override> >// no issue while throwing same exception> >void> m1()>throws> RuntimeException> >{> >System.out.println(>'From child1 m1()'>);> >}> }> class> Child2>extends> Parent {> >@Override> >// no issue while throwing subclass exception> >void> m1()>throws> ArithmeticException> >{> >System.out.println(>'From child2 m1()'>);> >}> }> class> Child3>extends> Parent {> >@Override> >// no issue while not throwing any exception> >void> m1()> >{> >System.out.println(>'From child3 m1()'>);> >}> }> class> Child4>extends> Parent {> >@Override> >// compile-time error> >// issue while throwing parent exception> >void> m1()>throws> Exception> >{> >System.out.println(>'From child4 m1()'>);> >}> }> |
>
>
산출
error: m1() in Child4 cannot override m1() in Parent void m1() throws Exception ^ overridden method does not throw Exception>
재정의 및 추상 메서드
인터페이스 또는 추상 클래스의 추상 메서드는 파생된 구체적 클래스에서 재정의되어야 하며 그렇지 않으면 컴파일 시간 오류가 발생합니다.
재정의 및 동기화/strictfp 방법
메서드에 동기화/엄격한 수식어가 있어도 재정의 규칙에는 영향이 없습니다. 즉, 동기화/엄격한 fp 메서드가 비동기화/엄격한 fp 메서드를 재정의할 수 있고 그 반대의 경우도 가능합니다.
메모:
- C++에서는 다음이 필요합니다. 가상 키워드 재정의를 달성하거나 런타임 다형성 . Java에서 메소드는 기본적으로 가상입니다.
- 다단계 메서드 재정의가 가능합니다.
자바
// A Java program to demonstrate> // multi-level overriding> // Base Class> class> Parent {> >void> show() { System.out.println(>'Parent's show()'>); }> }> // Inherited class> class> Child>extends> Parent {> >// This method overrides show() of Parent> >void> show() { System.out.println(>'Child's show()'>); }> }> // Inherited class> class> GrandChild>extends> Child {> >// This method overrides show() of Parent> >void> show()> >{> >System.out.println(>'GrandChild's show()'>);> >}> }> // Driver class> class> Main {> >public> static> void> main(String[] args)> >{> >Parent obj1 =>new> GrandChild();> >obj1.show();> >}> }> |
>
>
자바 필터 스트림산출
GrandChild's show()>
메소드 오버라이딩과 메소드 오버로딩
1. 과부하 서명이 다른 동일한 방법에 관한 것입니다. 재정의는 거의 동일한 방법과 동일한 서명이지만 상속을 통해 연결된 다른 클래스입니다.
2. 오버로딩은 컴파일러 시간 다형성의 예이고 재정의는 런타임 다형성의 예입니다. 다형성 .
Java 메서드 재정의에 대한 FAQ
Q1. 메서드 재정의란 무엇입니까?
앞서 설명한 것처럼 재정의된 메서드를 사용하면 Java에서 다음을 지원할 수 있습니다. 런타임 다형성 . 다형성은 한 가지 이유 때문에 객체 지향 프로그래밍에 필수적입니다. 다형성을 통해 일반 클래스는 모든 파생 클래스에 공통되는 메서드를 지정할 수 있고 하위 클래스는 해당 메서드 중 일부 또는 전부의 특정 구현을 정의할 수 있습니다. 재정의된 메서드는 Java가 다형성의 단일 인터페이스, 다중 메서드 측면을 구현하는 또 다른 방법입니다. 동적 메소드 디스패치 객체 지향 설계가 코드 재사용 및 견고성에 영향을 미치는 가장 강력한 메커니즘 중 하나입니다. 깔끔한 추상 인터페이스를 유지하면서 다시 컴파일하지 않고도 새 클래스의 인스턴스에서 메서드를 호출할 수 있는 코드 라이브러리가 존재하는 기능은 매우 강력한 도구입니다. 재정의된 메서드를 사용하면 파생 클래스 개체의 유형을 알지 못해도 파생 클래스의 메서드를 호출할 수 있습니다.
Q2. 메소드 재정의를 언제 적용해야 합니까? (예시 포함)
재정의 및 계승 : 다형성을 성공적으로 적용하는 열쇠 중 하나는 슈퍼클래스와 서브클래스가 하위 클래스에서 상위 클래스로 이동하는 계층 구조를 형성한다는 점을 이해하는 것입니다. 올바르게 사용하면 슈퍼클래스는 하위클래스에서 직접 사용할 수 있는 모든 요소를 제공합니다. 또한 파생 클래스가 자체적으로 구현해야 하는 메서드도 정의합니다. 이를 통해 하위 클래스는 메서드를 유연하게 정의할 수 있으면서도 일관된 인터페이스를 적용합니다. 따라서 상속과 재정의된 메서드를 결합함으로써 슈퍼클래스는 모든 하위 클래스에서 사용할 메서드의 일반적인 형식을 정의할 수 있습니다. 메서드 재정의를 사용하는 보다 실용적인 예를 살펴보겠습니다. 조직의 직원 관리 소프트웨어를 생각해 보세요. 코드에 간단한 기본 클래스 Employee가 있고 클래스에 raiseSalary(), transfer(), Promotion() 등과 같은 메서드가 있다고 가정합니다. Manager, Engineer, ..etc에는 기본 클래스 Employee에 있는 메서드 구현이 있을 수 있습니다. 우리의 완전한 소프트웨어에서는 모든 직원 목록을 전달하고 직원 유형을 알지 못한 채 적절한 메소드를 호출하기만 하면 됩니다. 예를 들어, 직원 목록을 반복하여 모든 직원의 급여를 쉽게 인상할 수 있습니다. 모든 유형의 직원은 해당 클래스에 해당 논리를 가질 수 있습니다. 특정 직원 유형에 대해 raiseSalary()가 있으면 해당 메서드만 호출되므로 걱정할 필요가 없습니다.
자바
// Java program to demonstrate application>// of overriding in Java>// Base Class>class>Employee {>>public>static>int>base =>10000>;>>int>salary() {>return>base; }>}>// Inherited class>class>Manager>extends>Employee {>>// This method overrides salary() of Parent>>int>salary() {>return>base +>20000>; }>}>// Inherited class>class>Clerk>extends>Employee {>>// This method overrides salary() of Parent>>int>salary() {>return>base +>10000>; }>}>// Driver class>class>Main {>>// This method can be used to print the salary of>>// any type of employee using base class reference>>static>void>printSalary(Employee e)>>{>>System.out.println(e.salary());>>}>>public>static>void>main(String[] args)>>{>>Employee obj1 =>new>Manager();>>// We could also get type of employee using>>// one more overridden method.loke getType()>>System.out.print(>'Manager's salary : '>);>>printSalary(obj1);>>Employee obj2 =>new>Clerk();>>System.out.print(>'Clerk's salary : '>);>>printSalary(obj2);>>}>}>>>산출Manager's salary : 30000 Clerk's salary : 20000>
관련 기사
- Java의 동적 메소드 디스패치 또는 런타임 다형성
- Object 클래스의 equals() 메서드 재정의
- Object 클래스의 toString() 메서드 재정의
- 자바에서 오버로딩
- Java 프로그램의 출력 | 세트 18(재정의)