Home >Java >javaTutorial >Can we override a protected method in Java?

Can we override a protected method in Java?

WBOY
WBOYforward
2023-08-28 15:25:071178browse

Can we override a protected method in Java?

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 .

Example

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();
   }
}

Output

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete