Computer >> 컴퓨터 >  >> 프로그램 작성 >> Java

Java에서 보호된 메서드를 재정의할 수 있습니까?


, 수퍼클래스의 보호된 메소드는 재정의될 수 있습니다. 하위 클래스에 의해. 수퍼클래스 메소드가 보호되는 경우 하위 클래스 재정의 메소드는 보호될 수 있습니다. 또는 공개 (그러나 기본값은 아님 또는 비공개 ) 이는 하위 클래스를 의미합니다. 재정의된 메서드는 약한 액세스 지정자를 가질 수 없습니다. .

예시

class A {
   protected void protectedMethod() {
      System.out.println("superclass protected method");
   }
}
class B extends A {
   protected void protectedMethod() {
      System.out.println("subclass protected method");
   }
}
public class Test {
   public static void main(String args[]) {
      B b = new B();
      b.protectedMethod();
   }
}

출력

subclass protected method