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:

  1. If one wishes to access a protected modifier outside a package, then inheritance is needed to be applied.
  2. Protecting a constructor prevents the users from creating the instance of the class, outside the package.
  3. During overriding, when a variable or method is protected, it can be overridden to other subclass using either a public or protected modifier only.
  4. 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



filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate
// protected modifier
  
package p1;
  
// Class A
public class A {
    protected void display()
    {
        System.out.println("GeeksforGeeks");
    }
}

chevron_right


Package p2

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate
// protected modifier
  
package p2;
  
// import all classes in package p1
import p1.*;
  
// Class B is a subclass of A
class B extends A {
    public static void main(String args[])
    {
        B obj = new B();
        obj.display();
    }
}

chevron_right


Output:

GeeksforGeeks

Now let’s try to analyse different conditions of access:

  1. 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

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to illustrate
    // protected modifier
      
    package p1;
      
    // Class A
    public class A {
        protected void display()
        {
            System.out.println("GeeksforGeeks");
        }
    }

    chevron_right

    
    

    Package p2

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to illustrate
    // protected modifier
      
    package p2;
      
    // import all classes in package p1
    import p1.*;
      
    // Class B is a subclass of A
    class B {
        public static void main(String args[])
        {
            B obj = new B();
            obj.display();
        }
    }

    chevron_right

    
    

    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)
  2. Accessing a protected class: In this example we tried to access a protected class A resulting in error.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to illustrate
    // protected modifier
      
    package p1;
      
    // Class A
    protected class A {
        void display()
        {
            System.out.println("GeeksforGeeks");
        }
    }

    chevron_right

    
    

    Package p2

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to illustrate
    // protected modifier
      
    package p2;
      
    // import all classes in package p1
    import p1.*;
      
    // Class B is a subclass of A
    class B extends A {
        public static void main(String args[])
        {
            B obj = new B();
            obj.display();
        }
    }

    chevron_right

    
    

    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)
  3. 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

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to illustrate
    // protected modifier
      
    package p1;
      
    // Class A
    public class A {
        protected void display()
        {
            System.out.println("GeeksforGeeks");
        }
    }

    chevron_right

    
    

    class C

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to illustrate
    // protected modifier
      
    // Class C is a subclass of A
    public class C {
        public static void main(String args[])
        {
            A obj = new A();
            obj.display();
        }
    }

    chevron_right

    
    

    Output:

    GeeksforGeeks
  4. 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

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to illustrate
    // protected modifier
      
    package p1;
      
    // Class A
    public class A {
        protected void display()
        {
            System.out.println("GeeksforGeeks");
        }
    }

    chevron_right

    
    

    Package p2

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to illustrate
    // protected modifier
      
    package p2;
      
    // import all classes in package p1
    import p1.*;
      
    // Class B is a subclass of A
    class B extends A {
        public static void main(String args[])
        {
            B obj = new B();
            obj.display();
        }
    }

    chevron_right

    
    

    Output:

    GeeksforGeeks
  5. 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

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to illustrate
    // protected modifier
    package p1;
      
    // Class A
    public class A {
        protected void display()
        {
            System.out.println("Class A");
        }
    }

    chevron_right

    
    

    class C

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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();
        }
    }

    chevron_right

    
    

    Output:

    Class C

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.