Protected Keyword in Java with Examples
Last Updated :
08 Jan, 2024
Access modifiers in Java help to restrict the scope of a class, constructor, variable, method, or data member. There are four types of access modifiers available in java. The access of various modifiers can be seen in the following table below as follows:

The protected keyword in Java refers to one of its access modifiers. The methods or data members declared as protected can be accessed from
- Within the same class.
- Subclasses of the same packages.
- Different classes of the same packages.
- Subclasses of different packages.
There are some certain important points to be remembered as follows:
- If one wishes to access a protected modifier outside a package, then inheritance is needed to be applied.
- Protecting a constructor prevents the users from creating the instance of the class, outside the package.
- During overriding, when a variable or method is protected, it can be overridden to other subclass using either a public or protected modifier only.
- Outer class and interface cannot be protected.
Implementation: Here we will be creating two packages p1 and p2. Class A in p1 is made public, to access it in p2. The method displayed in class A is protected and class B is inherited from class A and this protected method is then accessed by creating an object of class B.
Example 1: Package p1
Java
package p1;
public class A {
protected void display()
{
System.out.println( "GeeksforGeeks" );
}
}
|
Package p2
Java
package p2;
import p1.*;
class B extends A {
public static void main(String args[])
{
B obj = new B();
obj.display();
}
}
|
Output:
GeeksforGeeks
Now let us try to analyze different conditions of access:
- Calling protected function without extending the parent class
- Accessing a protected class
- Accessing display function from the same package but different
- Accessing display function from a different package
- Accessing a protected class by overriding to sub-class within the same package
A. Calling Protected Function Without Extending the Parent Class
Here we will create two packages p1 and p2. Class A in p1 is made public, to access it in p2. The method displayed in class A is protected. But the code will not be able to access the function “display” since the child class has not inherited its value from the main class and will throw an exception as shown.
Example 1-A: Package p1
Java
package p1;
public class A {
protected void display()
{
System.out.println( "GeeksforGeeks" );
}
}
|
Example 1-B: Package p2
Java
package p2;
import p1.*;
class B {
public static void main(String args[])
{
B obj = new B();
obj.display();
}
}
|
Output:
Exception in thread "main"
java.lang.RuntimeException:
Uncompilable source code -
Erroneous sym type: p2.B.display
at p2.B.main(B.java:16)
B: Accessing a Protected Class
Here we are trying to access a protected class A resulting in an error.
Example A:
Java
package p1;
protected class A {
void display()
{
System.out.println( "GeeksforGeeks" );
}
}
|
Example B: Package p2
Java
package p2;
import p1.*;
class B extends A {
public static void main(String args[])
{
B obj = new B();
obj.display();
}
}
|
Output: This will throw an error
Exception in thread "main"
java.lang.RuntimeException:
Uncompilable source code -
Erroneous sym type: p2.B.display
at p2.B.main(B.java:16)
C: Accessing Display Function From the Same Package But Different Class
Implementation: In this example, we have access to access a protected function “display” from the same package but a different class
Example A: Package p1
Java
package p1;
public class A {
protected void display()
{
System.out.println( "GeeksforGeeks" );
}
}
|
Example B: class C
Java
public class C {
public static void main(String args[])
{
A obj = new A();
obj.display();
}
}
|
Output:
GeeksforGeeks
D: Accessing Display Function From a Different Package
Here we have tried to access the protected function display from a different package by inheritance and extending the class.
Example A: Package p1
Java
package p1;
public class A {
protected void display()
{
System.out.println( "GeeksforGeeks" );
}
}
|
Example B: Package p2
Java
package p2;
import p1.*;
class B extends A {
public static void main(String args[])
{
B obj = new B();
obj.display();
}
}
|
Output:
GeeksforGeeks
E: Accessing a Protected Class by Overriding to Sub-Class Within the Same Package
Here we have designed two classes A and C, where class C is the overridden one.
Example A: class A
Java
package p1;
public class A {
protected void display()
{
System.out.println( "Class A" );
}
}
|
Example B: class C
Java
public class C extends A {
protected void display()
{
System.out.println( "Class C" );
}
public static void main(String args[])
{
C obj1 = new C();
obj1.display();
}
}
|
Output:
Class C
Similar Reads
Properties keys() method in Java with Examples
The keys() method of Properties class is used to get the enumeration of the keys in this Properties object. This enumeration can be used to traverse and iterate the keys sequentially. Syntax: public Enumeration keys() Parameters: This method accepts no parameters Returns: This method returns an Enum
2 min read
Provider keys() method in Java with Examples
The keys() method of java.security.Provider class is used to return an enumeration of the keys in this hashtable. Syntax: public Enumeration keys() Return Value: This method returns an enumeration of the keys in this hashtable. Below are the examples to illustrate the keys() method: Program 1: Java
2 min read
Provider get() method in Java with Examples
The get() method of java.security.Provider class is used to return the value to which the specified key is mapped, or null if this map contains no mapping for the key. More formally, if this map contains a mapping from a key k to a value v such that (key.equals(k)), then this method returns v; other
3 min read
KeyStore getKey() method in Java with Examples
The getKey() method of java.security.KeyStore class is used to get the key associated with the given alias, using the given password to recover it. Syntax: public final Key getKey(String alias, char[] password) throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException Parameter:
3 min read
Provider values() method in Java with Examples
The values() method of java.security.Provider class is used to return an unmodifiable Collection view of the property values contained in this provider.Syntax: public Collection<Object> values() Return Value: This method returns a collection view of the values contained in this map.Below are t
3 min read
Reader ready() method in Java with Examples
The ready() method of Reader Class in Java is used to check whether this Reader is ready to be read or not. It returns a boolean which states if the reader is ready. Syntax: public void ready() Parameters: This method does not accepts any parameters Return Value: This method returns a boolean value
3 min read
KeyStore aliases() method in Java with Examples
The aliases() method of java.security.KeyStore class provides alias name associated with this keystore object. Syntax: public final Enumeration aliases() throws KeyStoreException Return Value: This method returns the list of all alias associated with this keystore object as an Enum object.Exception:
3 min read
Pattern flags() method in Java with Examples
The flags() method of the Pattern class in Java is used to return the pattern's match flags. The Match flags are a bit mask that may include CASE_INSENSITIVE, MULTILINE, DOTALL, UNICODE_CASE, CANON_EQ, UNIX_LINES, LITERAL, UNICODE_CHARACTER_CLASS and COMMENTS Flags. Syntax: public int flags() Parame
2 min read
Provider toString() method in Java with Examples
The toString() method of java.security.Provider class is used to return a string with the name and the version number of this provider. Syntax: public String toString() Return Value: This method returns the string with the name and the version number for this provider. Below are the examples to illu
2 min read
Field set() method in Java with Examples
The set() method of java.lang.reflect.Field is used to set the value of the field represented by this Field object on the specified object argument to the specified new value passed as parameter. The new value is automatically unwrapped if the underlying field has a primitive type. If the field is s
4 min read