
- 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
What is the scope of protected access modifier in Java?
When a variable, method or constructor that are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class.
The protected access modifier cannot be applied to class and interfaces. Methods, fields can be declared protected, however methods and fields in a interface cannot be declared protected.
Protected access gives the subclass a chance to use the helper method or variable, while preventing a nonrelated class from trying to use it.
Example
The following parent class uses protected access control, to allow its child class override openSpeaker() method −
class AudioPlayer { protected boolean openSpeaker(Speaker sp) { // implementation details } } class StreamingAudioPlayer { boolean openSpeaker(Speaker sp) { // implementation details 8. Java MODIFIERS } }
Here, if we define openSpeaker() method as private, then it would not be accessible from any other class other than AudioPlayer. If we define it as public, then it would become accessible to all the outside world. But our intention is to expose this method to its subclass only, that’s why we have used protected modifier.
- Related Questions & Answers
- protected access modifier in Java
- What is the scope of public access modifier in Java?
- What is the scope of default access modifier in Java?
- What is the scope of private access modifier in Java?
- Why Protected access modifier used in Java?
- private access modifier in Java
- default access modifier in Java
- public access modifier in Java
- What is the scope of a protected member variable of a class in C#?
- Demonstrate Private access modifier in Java
- What is the scope of a protected internal member variable of a class in C#?
- Can we declare main() method as private or protected or with no access modifier in java?
- WiFi Protected Access (WPA) and WiFi Protected Access 2 (WPA2)
- What are the differences between protected and default access specifiers in Java?
- What are private, public, default and protected access Java modifiers?