

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Can we override a protected method in Java?
Yes, the protected method of a superclass can be overridden by a subclass. If the superclass method is protected, the subclass overridden method can have protected or public (but not default or private) which means the subclass overridden method can not have a weaker access specifier.
Example
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(); } }
Output
subclass protected method
- Related Questions & Answers
- Can we override a start() method in Java?
- Can we override the static method in Java?
- Can we override the main method in java?
- Can we override the equals() method in Java?
- Can we overload or override a static method in Java?
- Can we override a private or static method in Java
- Can we override only one method while implementing Java interface?
- Can we override private methods in Java
- Can we override final methods in Java?
- Can we override default methods in Java?
- Can we to override a catch block in java?
- Can we declare an abstract method, private, protected, public or default in java?
- Can we Overload or Override static methods in Java?
- Why can’t we override static methods in Java?
- Can we declare the variables of a Java interface private and protected?
Advertisements