In Java, access modifiers control the visibility of classes, methods, and variables. "Protected" is one of these modifiers that restricts the accessibility of a class member. It is only visible to its own class, subclasses, and classes in the same package. It plays a crucial role in writing efficient and secure Java code.

Java protected keyword

The protected keyword in Java is an access modifier used to restrict the visibility of a class, method, or variable. When a class member is marked as protected, it can be accessed by the members of its own class, its subclasses, and classes in the same package. However, it cannot be accessed by any class outside the package.

Protected Class

Java also allows us to declare a protected class. A protected class is only accessible to its subclasses and classes in the same package. The keyword protected can be used with the class keyword to define a protected class.

protected class MyProtectedClass {
   // code here
}

Syntax of the protected Keyword

To declare a member with the protected access modifier, the syntax is straightforward:


[access_modifier] [data_type] member_name;

// Example
protected int id;
protected void display() {
    System.out.println("Protected Method");
}
    

When used, protected members are accessible within the same package and by subclasses in other packages.

Implementation of protected keywords in Java

Let's take a look at a simple example that demonstrates the implementation of the protected keyword in Java:

class A {
   protected int x = 10;
}

class B extends A {
   void display() {
      System.out.println("The value of x is: " + x);
   }
}

class Main {
   public static void main(String[] args) {
      B obj = new B();
      obj.display();
   }
}
In the above code, we have two classes A and B. Class A has a protected variable named x. The class B extends class A and has a method named display, which simply prints the value of x. In the main method, we create an object of class B and call the display method. The output of the above code will be:
The value of x is: 10
Since the variable x is marked as protected in class A, it is accessible in class B, which extends class A.

Test Exercise

Try declaring a “public” class and use class A or B there. Test if you can access their data variables. Java protected keyword - 1

Examples of protected Keyword in Different Contexts

1. Using protected with Variables


class Parent {
    protected String name = "ParentName";
}

class Child extends Parent {
    void printName() {
        System.out.println("Name: " + name);
    }
}

public class Main {
    public static void main(String[] args) {
        Child child = new Child();
        child.printName(); // Accesses protected member
    }
}
    

2. Using protected with Methods


class Parent {
    protected void showMessage() {
        System.out.println("Protected method in Parent class");
    }
}

class Child extends Parent {
    void display() {
        showMessage(); // Accessing protected method
    }
}

public class Main {
    public static void main(String[] args) {
        Child child = new Child();
        child.display();
    }
}
    

3. Using protected with Constructors


class Parent {
    protected Parent() {
        System.out.println("Protected Constructor");
    }
}

class Child extends Parent {
    Child() {
        super(); // Accessing protected constructor
    }
}

public class Main {
    public static void main(String[] args) {
        Child child = new Child();
    }
}
    

Using protected in Inter-Package Context

When working across packages, the protected keyword allows subclass access but restricts access from non-subclass classes.

Example:


// In package1
package package1;

public class Parent {
    protected void display() {
        System.out.println("Protected method from Parent class");
    }
}

// In package2
package package2;

import package1.Parent;

public class Child extends Parent {
    public void callDisplay() {
        display(); // Allowed due to subclassing
    }

    public static void main(String[] args) {
        Child child = new Child();
        child.callDisplay();
    }
}
    

Using protected with Outer Classes

In Java, outer classes cannot be declared as protected. However, inner classes can be protected, and this can be demonstrated as follows:


class Outer {
    protected class Inner {
        void greet() {
            System.out.println("Hello from Protected Inner Class");
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Outer.Inner inner = new Outer().new Inner();
        inner.greet();
    }
}
    

Conclusion

The protected keyword in Java is an important access modifier that provides limited access to class members. When used properly, it can help to keep the code secure and organized. It is worth noting that the protected keyword is not often used in practice, but it is essential to know its implementation and usage to write efficient and secure Java code.