Protected Keyword in Java with Examples

01 May, 2022
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.
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
// Java program to illustrate // protected modifier   package p1;   // Class A public class A {     protected void display()     {         System.out.println("GeeksforGeeks");     } }
 Package p2 
Java
// Java program to illustrate// protected modifier  package p2;  // import all classes in package p1import p1.*;  // Class B is a subclass of Aclass 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 classAccessing a protected classAccessing display function from the same package but different Accessing display function from a different packageAccessing a protected class by overriding to sub-class within the same package
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
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
// Java program to illustrate Protected Modifier  package p1;  // Class Apublic class A {      // Method    protected void display()    {        // Print statement        System.out.println("GeeksforGeeks");    }}
Example 1-B: Package p2 
Java
// Java program to illustrate// protected modifier  package p2;  // import all classes in package p1import p1.*;  // Class B is a subclass of Aclass 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)
Here we are trying to access a protected class A resulting in an error. 
Example A:
Java
// Java program to illustrate// protected modifier  package p1;  // Class Aprotected class A {    void display()    {        System.out.println("GeeksforGeeks");    }}
Example B: Package p2 
Java
// Java program to illustrate// protected modifier  package p2;  // import all classes in package p1import p1.*;  // Class B is a subclass of Aclass 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)
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
// Java program to illustrate// protected modifier  package p1;  // Class Apublic class A {    protected void display()    {        System.out.println("GeeksforGeeks");    }}
Example B: class C 
Java
// Java program to illustrate// protected modifier  // Class C is a subclass of Apublic class C {    public static void main(String args[])    {        A obj = new A();        obj.display();    }}
Output: 
GeeksforGeeks
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
// Java program to illustrate// protected modifier  package p1;  // Class Apublic class A {    protected void display()    {        System.out.println("GeeksforGeeks");    }}
Example B: Package p2 
Java
// Java program to illustrate// protected modifier  package p2;  // import all classes in package p1import p1.*;  // Class B is a subclass of Aclass B extends A {    public static void main(String args[])    {        B obj = new B();        obj.display();    }}
Output: 
GeeksforGeeks
Here we have designed two classes A and C, where class C is the overridden one. 
Example A: class A 
Java
// Java program to illustrate// protected modifierpackage p1;  // Class Apublic class A {    protected void display()    {        System.out.println("Class A");    }}
Example B: class C 
Java
// Java program to illustrate// protected modifier  public class C extends A {      // overridden function    protected void display()    {        System.out.println("Class C");    }    public static void main(String args[])    {        C obj1 = new C();        obj1.display();    }}
Output: 
Class C
solankimayank
access modifiers
Java-keyword
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Interfaces in Java
Stream In Java
ArrayList in Java
Collections in Java
Singleton Class in Java
Multidimensional Arrays in Java
Set in Java
Stack Class in Java
Initializing a List in Java
Introduction to Java