Skip to main content
Documents
Share
LinkedIn
Facebook
Twitter
Copy
Java keywords

protected Keyword in Java

The protected keyword in Java is an access modifier used for member variables and methods. It provides a level of access control that allows access within the same package and by subclasses, even if they are in different packages.

Usage

The protected access modifier is used to restrict access to members of a class such that they are accessible within their own package and by subclasses. This is particularly useful for creating a controlled inheritance structure.

Syntax

protected dataType variableName;
protected returnType methodName() {
    // method body
}
  • dataType: The type of the variable.
  • variableName: The name of the variable.
  • returnType: The return type of the method.
  • methodName: The name of the method.

Examples

Example 1: Protected Variable

package com.example;

public class Parent {
    protected int protectedVariable = 100;
}

package com.example;

public class Child extends Parent {
    public void display() {
        System.out.println("Protected Variable: " + protectedVariable);
    }
}

In this example, protectedVariable is declared with the protected keyword in the Parent class. The Child class, which is a subclass of Parent, can access protectedVariable and print its value.

Example 2: Protected Method

package com.example;

public class Parent {
    protected void protectedMethod() {
        System.out.println("This is a protected method.");
    }
}

package com.example;

public class Child extends Parent {
    public void callProtectedMethod() {
        protectedMethod();
    }
}

Here, protectedMethod is a protected method in the Parent class. The Child class can call this method because it is a subclass of Parent.

Example 3: Access in Different Package

package com.example.parent;

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

package com.example.child;

import com.example.parent.Parent;

public class Child extends Parent {
    public void callProtectedMethod() {
        protectedMethod();
    }
}

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

In this example, the Parent class is in the com.example.parent package, and the Child class is in the com.example.child package. The Child class can still access the protectedMethod of the Parent class because it is a subclass.

Tips and Best Practices

  • Controlled Inheritance: Use protected to allow subclasses to access members while keeping them hidden from other classes in different packages.
  • Package Access: Remember that protected members are also accessible to other classes in the same package.
  • Encapsulation: Use protected to balance encapsulation and inheritance, exposing only necessary members to subclasses.
  • Avoid Overexposure: Be cautious not to overuse protected, as it might expose too much of the class's internal workings to subclasses and other classes in the same package.
  • Documentation: Clearly document the intended use of protected members to avoid misuse or unintended access.
  • Access through Subclass Instances: Understand that protected members can be accessed through instances of subclasses, even if the instance is declared in a different package.