Protected Keyword in Java with Examples
Overview
In Java programming, access modifiers play a crucial role in controlling the visibility and accessibility of class members. These modifiers determine which parts of your codebase can interact with specific variables, methods, and classes. Java offers a quartet of access modifiers— public, private, default, and protected—each guiding the accessibility spectrum.
default | private | protected | public | |
---|---|---|---|---|
same class | yes | yes | yes | yes |
same package subclass | yes | no | yes | yes |
same package non-subclass | yes | no | yes | yes |
different package subclass | no | no | no | yes |
different package non-subclass | no | no | no | yes |
Among these modifiers, the protected keyword holds a distinctive position, offering a controlled bridge between encapsulation and inheritance. In this article, we will explore the protected keyword. We will understand the significance of protected in Java within class hierarchies, how it enables selective member exposure, and discover practical use cases that utilises its power to enhance code flexibility and maintainability.
What is Protected Keyword in Java
The protected keyword in Java serves as an access modifier, influencing how methods or data members can be interacted with. When designated as protected, these components offer accessibility under several conditions:
- Within the Same Class:
Members marked as protected can be accessed from within the very class they are defined in. - Subclasses of the Same Package:
Subclasses, residing within the same package as the class where the protected members are declared, are also granted access. - Different Classes of the Same Package:
Protected elements can be accessed by other classes within the same package, regardless of any subclass relationship. - Subclasses of Different Packages:
Even in cases where the subclasses are situated in different packages, the protected members can still be accessed.
Important points to remember regarding the protected in Java:
- To access protected members from outside the package, inheritance must be employed. This implies that the accessing class must be a subclass of the class declaring the protected member.
- Using the protected modifier for a constructor prevents the creation of class instances from outside the package.
- During method or variable overriding, a protected member can be overridden with either a public or protected modifier in the subclass.
- The protected access modifier cannot be applied to outer classes or interfaces.
Implementations of Protected Keyword in Java
Now we will create two packages p1 and p2. class A in p1 is made public and it will be accessed in p2. The method displayed in class A is protected and class B is inherited from class A. The protected method is then accessed by creating an object of class B.
Package p1
Code:
Package p2
Code:
Output:
Now let us analyze the access of protected members in different conditions:
- Invoking a protected function without inheriting from the parent class.
- Accessing a protected class.
- Accessing the displayMessage() function from a different class within the same package.
- Accessing the displayMessage() function from an external package.
- Accessing a protected class by overriding to sub-class within the same package
Attempting to Call a Protected Function without Extending the Parent Class?
Here we have two packages p1 and p2. class A in p1 is made public and is accessed in p2. The method displayMessage() in class A is protected. The code won't be able to access the function displayMessage() since the class B has not inherited its value from class A and will throw an exception.
Package p1
Code:
Package p2
Code:
Output: The following error will be thrown.
How to Access A Protected Class?
Here we are accessing a protected class A from class B that will cause an error.
Package p1
Code:
Package p2
Code:
Output:
The following error will be thrown.
Accessing a Function
From Same Package But Different Class
Here we are accessing a protected function displayMessage() from the same package but a different class.
Package p1
Code:
class C
Code:
Output:
From Different Package
Here we are accessing the protected function displayMessage() from a different package by inheritance and extending the class.
Package p1
Code:
Package p2
Code:
Output:
From Different Package
Here we are accessing the protected function displayMessage() from a different package by inheritance and extending the class.
Package p1
Code:
Package p2
Code:
Output:
How to Access a Protected Class by Overriding to Sub-Class Within the Same Package?
In this example, we have two classes: class A and class C, where class C is the overridden one.
class A
Code:
class C
Code:
Output:
FAQs
Q: What does the protected in Java do?
A: The protected in Java grants access to class members within the same package and to subclasses, even if they're in different packages.
Q: Can protected members be accessed from outside the package without inheritance?
A: No, accessing protected members from outside the package requires inheritance, ensuring controlled visibility.
Q: Can I use the protected in Java for outer classes or interfaces?
A: No, the protected access modifier is not applicable to outer classes or interfaces; it's intended for members within a class.
Conclusion
Here are key takeaways from this article on protected keyword in Java:
- The protected in Java strikes a balance between encapsulation and accessibility, catering to subclasses and same-package classes while preventing unrestricted external access.
- By enabling protected members to be inherited by subclasses, Java encourages the creation of extensible class hierarchies, enhancing code modularity and reuse.
- With the ability to share resources within the same package, the protected in Java promotes controlled collaboration among related classes, reducing tight coupling.
- Accessing protected members outside their package requires inheritance, reinforcing controlled interaction and minimizing unintended dependencies.
- Proper use of the protected in Java contributes to maintainable codebases by clarifying member visibility and facilitating predictable subclass behavior.
- Incorporating protected access aligns with core Object-Oriented Programming principles, such as encapsulation and inheritance, enhancing the overall design quality of Java applications.