Java protected keyword
A Java protected keyword is an access modifier. It can be assigned to variables, methods, constructors and inner classes.
Points to remember
- The protected access modifier is accessible within the package. However, it can also accessible outside the package but through inheritance only.
- We can’t assign protected to outer class and interface.
- If you make any constructor protected, you cannot create the instance of that class from outside the package.
- If you are overriding any method, overridden method (i.e., declared in the subclass) must not be more restrictive.
- According to the previous point, if you assign protected to any method or variable, that method or variable can be overridden to sub-class using public or protected access modifier only.
Examples of protected keyword
Example 1
Let’s see an example to determine whether the protected variable is accessible or not outside the package.
Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: The field A.msg is not visible
Example 2
Let’s see an example to determine whether the protected variable is accessible or not outside the class and within the package.
Output:
Try to access the protected variable outside the class within the package
Example 3
Let’s see an example to determine whether the protected method is accessible or not outside the package.
Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method msg() from the type A is not visible
Example 4
Let’s see an example to determine whether the protected method is accessible or not outside the package using inheritance.
Output:
Try to access the protected method outside the package using inheritance
Example 5
Let’s see an example to determine whether we assign protected to the outer class.
Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Example 6
Let’s see an example to determine whether we create the instance of protected constructor from outside the class.
Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: The constructor A(String) is not visibles
Example 7
Let’s see an example to determine whether the protected method is overridden to sub-class using protected access modifier.
Output:
Try to access the overridden method
Example 8
Let’s see an example to determine whether the protected method is overridden to sub-class using private access modifier.
Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: Cannot reduce the visibility of the inherited method from A
Example 9
Let’s see an example to determine whether the protected method is overridden to sub-class using default access modifier.
Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: Cannot reduce the visibility of the inherited method from A
Example 10
Let’s see an example to determine whether the protected method is overridden to sub-class using public access modifier.
Output:
Try to access the overridden method