ใช่ วิธีการป้องกันของซูเปอร์คลาสสามารถ แทนที่ โดยคลาสย่อย หากเมธอด superclass ได้รับการปกป้อง เมธอด subclass overridden สามารถมี protected หรือ สาธารณะ (แต่ไม่ใช่ ค่าเริ่มต้น หรือ ส่วนตัว ) ซึ่งหมายถึง คลาสย่อย วิธีการแทนที่ไม่สามารถมีตัวระบุการเข้าถึงที่อ่อนแอกว่า .
ตัวอย่าง
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