is , the protected method of the super class can be overridden by the subclass . If the superclass method is protected, the subclass overridden method can have protected or public (but cannot have default or private ) ) This means that subclasses overridden methods cannot have weaker access specifiers .
class A { protected<strong> </strong>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
The above is the detailed content of Can we override a protected method in Java?. For more information, please follow other related articles on the PHP Chinese website!