Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Protected keyword in Java with examples

  • Difficulty Level : Hard
  • Last Updated : 28 Nov, 2019

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.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

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




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

Package p2




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

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




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

    Package p2




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

    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.




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

    Package p2






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

    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




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

    class C




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

    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




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

    Package p2




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

    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




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

    class C




    // 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



My Personal Notes arrow_drop_up
Recommended Articles
Page :