Protected keyword in Java with examples
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 same packages.
- Different classes of same packages.
- Subclasses of different packages.
The access of various modifiers can be seen from the following table.
Important Points:
- 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.
Let’s understand protected keyword with the help of an example:
Example: In this example, we will create two packages p1 and p2. Class A in p1 is made public, to access it in p2. The method display 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.
Package p1
// Java program to illustrate// protected modifier package p1; // Class Apublic class A { protected void display() { System.out.println("GeeksforGeeks"); }} |
Package p2
// 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’s try to analyse different conditions of access:
- Calling protected function without extending the parent class: In this example, we will create two packages p1 and p2. Class A in p1 is made public, to access it in p2. The method display 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.
Package p1// Java program to illustrate// protected modifierpackagep1;// Class ApublicclassA {protectedvoiddisplay(){System.out.println("GeeksforGeeks");}}Package p2
// Java program to illustrate// protected modifierpackagep2;// import all classes in package p1importp1.*;// Class B is a subclass of AclassB {publicstaticvoidmain(String args[]){B obj =newB();obj.display();}}This will throw an error of:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: p2.B.display at p2.B.main(B.java:16) - Accessing a protected class: In this example we tried to access a protected class A resulting in error.
// Java program to illustrate// protected modifierpackagep1;// Class AprotectedclassA {voiddisplay(){System.out.println("GeeksforGeeks");}}Package p2
// Java program to illustrate// protected modifierpackagep2;// import all classes in package p1importp1.*;// Class B is a subclass of AclassBextendsA {publicstaticvoidmain(String args[]){B obj =newB();obj.display();}}This will throw an error of:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: p2.B.display at p2.B.main(B.java:16) - Accessing display function from the same package but different class: In this example we have accessed to access a protected function “display” from the same package but different class
Package p1// Java program to illustrate// protected modifierpackagep1;// Class ApublicclassA {protectedvoiddisplay(){System.out.println("GeeksforGeeks");}}class C
// Java program to illustrate// protected modifier// Class C is a subclass of ApublicclassC {publicstaticvoidmain(String args[]){A obj =newA();obj.display();}}Output:
GeeksforGeeks
- Accessing display function from a different package: In this example we have tried to access the protected function display from a different package by inheritance and extending the class.
Package p1// Java program to illustrate// protected modifierpackagep1;// Class ApublicclassA {protectedvoiddisplay(){System.out.println("GeeksforGeeks");}}Package p2
// Java program to illustrate// protected modifierpackagep2;// import all classes in package p1importp1.*;// Class B is a subclass of AclassBextendsA {publicstaticvoidmain(String args[]){B obj =newB();obj.display();}}Output:
GeeksforGeeks
- Accessing a protected class by overriding to sub-class within same package: In this example we have designed two classes A and C, where class C is the overridden one.
class A// Java program to illustrate// protected modifierpackagep1;// Class ApublicclassA {protectedvoiddisplay(){System.out.println("Class A");}}class C
// Java program to illustrate// protected modifierpublicclassCextendsA {// overridden functionprotectedvoiddisplay(){System.out.println("Class C");}publicstaticvoidmain(String args[]){C obj1 =newC();obj1.display();}}Output:
Class C



