Back to Java
2026-02-065 min read

protected

Learn protected step by step with clear examples and exercises.

Why This Matters

Java provides access modifiers to control the visibility of classes, methods, variables, and constructors. One such access modifier is protected. In this tutorial, we will delve into understanding the protected keyword, its usage, and common mistakes to avoid when using it.

Why This Matters

The protected keyword plays a crucial role in object-oriented programming by offering more flexibility than private (only accessible within the same class) and public (accessible anywhere) access modifiers. It allows subclasses to access protected members of their superclass, even if those subclasses are in different packages. This is particularly useful when designing hierarchies of related classes, as it enables code reusability and maintains encapsulation.

Prerequisites

To fully grasp the concept of protected access modifier in Java, you should have a solid understanding of:

  • Classes and objects
  • Inheritance and polymorphism
  • Access modifiers (public, private, default)
  • Encapsulation

Core Concept

Definition and Scope

The protected keyword in Java grants access to a member within the same package as well as any subclass, regardless of their packages. This means that if a class is declared protected, it can be accessed by any class within its own package or by any subclass, even if those subclasses are located in different packages.

Example: Using Protected Members in Superclass and Subclass

Let's create an example to illustrate the usage of protected members in Java:

// Superclass with a protected variable
protected class Base {
protected int protectedVariable = 42;
}

// Subclass accessing protected variable from superclass
public class Derived extends Base {
public void printProtected() {
System.out.println("Protected variable: " + protectedVariable);
}
}

In the above example, we have a Base class with a protected variable protectedVariable. The Derived class is a subclass of Base, and it can access the protected variable using the printProtected() method. If you run this code, you will see the output:

Protected variable: 42

Worked Example

Let's consider a more complex example that demonstrates the use of protected members in a hierarchy of classes:

// Animal superclass with a protected method
protected class Animal {
public void walk() {
System.out.println("Animal is walking");
}
}

// Mammal subclass extending Animal and overriding walk method
public class Mammal extends Animal {
@Override
public void walk() {
System.out.println("Mammal is walking on four legs");
}
}

// Bird subclass extending Animal and implementing its own walk method
public class Bird extends Animal {
@Override
public void fly() {
System.out.println("Bird is flying");
}
}

// Main class testing the hierarchy
public class Main {
public static void main(String[] args) {
Mammal mammal = new Mammal();
mammal.walk(); // Output: Mammal is walking on four legs

Bird bird = new Bird();
bird.fly(); // Output: Bird is flying
}
}

In this example, we have an Animal superclass with a protected method walk(). We create two subclasses, Mammal and Bird, both extending the Animal class. The Mammal class overrides the walk() method to provide more specific behavior, while the Bird class implements its own fly() method. In the main class, we create instances of both subclasses and call their methods to demonstrate the usage of protected members in a hierarchical structure.

Common Mistakes

  1. Forgetting to declare a member as protected: If you want a member to be accessible by subclasses, you must explicitly declare it as protected.
// Incorrect: private variable not accessible by subclass
private class Base {
private int privateVariable = 42;
}

// Subclass cannot access the private variable
public class Derived extends Base {
public void printPrivate() {
// Compilation error: privateVariable has private access in Base
System.out.println("Private variable: " + privateVariable);
}
}
  1. Declaring a member as protected but not using it appropriately: Protected members should be used when you want to provide controlled access to subclasses while maintaining encapsulation within the superclass. If a protected member is not necessary, consider making it private or public instead.
  1. Misunderstanding the package visibility of protected members: Protected members can be accessed by any class within their own package as well as by any subclass, regardless of their packages. However, they are still not accessible from outside the package that contains the superclass.

Practice Questions

  1. What is the difference between public and protected access modifiers in Java? Provide examples to illustrate their usage.
  2. Create a class hierarchy with three classes: Animal, Mammal, and Cat. The Animal class should have a protected method called makeSound(). The Mammal class should extend the Animal class, override the makeSound() method, and add a new method called eatFood(). The Cat class should extend the Mammal class and provide its own implementation of both methods.
  3. Explain how protected members help maintain encapsulation in Java. Provide an example to illustrate your answer.

FAQ

  1. Can a protected member be accessed by any class outside its package?
  • No, protected members can only be accessed within their own package and by any subclass, regardless of the packages they are located in. They are not accessible from outside the package that contains the superclass.
  1. What is the purpose of using protected access modifiers in Java?
  • Protected access modifiers offer more flexibility than private (only accessible within the same class) and public (accessible anywhere) access modifiers by allowing subclasses to access protected members of their superclass, even if those subclasses are in different packages. This is particularly useful when designing hierarchies of related classes, as it enables code reusability and maintains encapsulation.
  1. Can a protected member be accessed by a class that is not a subclass?
  • No, protected members can only be accessed within their own package or by any subclass, regardless of the packages they are located in. They cannot be accessed directly by classes that are not subclasses.
protected | Java | XQA Learn