logo

Java에서 정적 메서드를 재정의할 수 있나요?

자바에서는 재정의 그리고 과부하 가장 중요한 두 가지 기능은 객체 지향 프로그래밍 . 이 기능은 프로그래머가 달성을 원할 때 사용됩니다. 다형성 . 질문, Java에서 정적 메서드를 재정의할 수 있나요? 또는 Java에서 정적 메서드를 오버로드할 수 있나요? 가장 중요한 질문 두 가지는 자바 인터뷰 . 이번 섹션에서는 오버로딩(overloading)과 오버라이딩(overriding)을 간단히 이해하겠습니다. 우리는 또한의 답변을 설명했습니다 Java에서 정적 메소드를 대체할 수 없는 이유는 무엇입니까?

정적 방법

메소드 이름 앞에 static 키워드가 있는 메소드를 정적 방법 . 그것은 또한 클래스 수준 방법 . 정적 메서드의 복사본은 클래스의 모든 개체에서 공유됩니다.

 public static int sum() { } 

클래스 이름을 사용하여 정적 메서드를 호출할 수 있습니다. 예를 들어, 수학.abs(a) 방법 . 이 메서드는 전달된 인수의 절대값을 반환합니다. 정적 메서드는 인스턴스 변수나 메서드에 액세스할 수 없습니다.

메소드 재정의

의 특징입니다 객체 지향 프로그래밍 . 런타임 다형성을 달성하는 데 사용됩니다. 하위 클래스는 상위 클래스에서 이미 제공한 메소드의 특정 구현을 제공합니다. 메서드 재정의. 부모 클래스와 자식 클래스의 메서드 시그니처는 동일해야 합니다. ~ 안에 메서드 재정의 , 어떤 메서드를 실행할지는 런타임에 결정됩니다. 우리가 호출한 객체에 따라 결정이 내려집니다.

메소드 오버로딩

이는 객체지향 프로그래밍의 특징이기도 합니다. 컴파일 타임 다형성을 달성하는 데 사용됩니다. 이를 통해 동일한 메소드 이름이지만 다른 서명을 사용할 수 있습니다. 클래스에 이름은 같지만 메서드 시그니처가 다른 메서드가 두 개 이상 있는 경우 이를 다음과 같이 부릅니다. 메소드 오버로딩 .

우리는 오버로딩과 오버라이딩이 무엇인지 배웠습니다. 이제 우리는 요점으로 이동합니다.

정적 메서드를 오버로드할 수 있나요?

정답은 . 정적 메서드를 오버로드할 수 있습니다. 하지만 메소드 서명은 달라야 한다는 점을 기억하세요. 예를 들어, 다음 Java 프로그램을 고려하십시오.

OverloadStaticMethodExample1.java

 public class OverloadStaticMethodExample1 { //static method public static void display() { System.out.println('Static method called.'); } //overloaded static method public static void display(int x) { System.out.println('An overloaded static method called.'); } //main method public static void main(String args[]) { //calling static method by using the class name OverloadStaticMethodExample1.display(); OverloadStaticMethodExample1.display(160); } } 

산출:

 Static method called. An overloaded static method called. 

여기서 다음과 같은 질문이 생깁니다. 정적 키워드만 다른 경우 메소드를 오버로드할 수 있습니까?

정답은 아니요. 정적 키워드만 다른 경우 두 메서드를 재정의할 수 없습니다. 예를 들어, 다음 Java 프로그램을 고려하십시오.

OverloadStaticMethodExample2.java

 public class OverloadStaticMethodExample2 { //static method public static void sum(int a, int b) { int c=a+b; System.out.println('The sum is: '+c); } //non-static method public void sum(int a, int b) { int c=a+b; System.out.println('The sum is: '+c); } //main method public static void main(String args[]) { //calling static method by using the class name OverloadStaticMethodExample2.sum(12, 90); } } 

위 프로그램을 컴파일하면 다음과 같은 오류가 표시됩니다.

 error: method sum(int,int) is already defined in class OverloadStaticMethodExample2 

정적 메서드를 재정의할 수 있나요?

아니요, 메서드 재정의는 런타임 시 동적 바인딩을 기반으로 하고 정적 메서드는 컴파일 시 정적 바인딩을 사용하여 결합되므로 정적 메서드를 재정의할 수 없습니다. 따라서 정적 메서드를 재정의할 수 없습니다.

메서드 호출은 정적 메서드를 호출하는 개체 유형에 따라 달라집니다. 그 뜻은:

  • 부모 클래스 객체를 사용하여 정적 메서드를 호출하면 원래 정적 메서드는 부모 클래스에서 호출됩니다.
  • 자식 클래스 객체를 사용하여 정적 메서드를 호출하면 자식 클래스의 정적 메서드가 호출됩니다.

다음 예제에서 ParentClass에는 display()라는 정적 메서드가 있고 ChildClass에도 동일한 메서드 시그니처가 있습니다. 파생 클래스(ChildClass)의 메서드는 기본 클래스의 메서드를 숨깁니다. 예를 보자.

OverloadStaticMethodExample3.java

 public class OverloadStaticMethodExample3 { public static void main(String args[]) { ParentClass pc = new ChildClass(); //calling display() method by parent class object pc.display(); } } //parent class class ParentClass { //we cannot override the display() method public static void display() { System.out.printf('display() method of the parent class.'); } } //child class class ChildClass extends ParentClass { //the same method also exists in the ParentClass //it does not override, actually it is method hiding public static void display() { System.out.println('Overridden static method in Child Class in Java'); } } 

산출:

 display() method of the parent class.