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

Can we override a protected method in Java?

WBOY
Release: 2023-08-28 15:25:07
forward
1171 people have browsed it

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

Output

subclass protected method
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template