The protected access modifier lets you share class members with related classes but keeps them hidden from unrelated ones.
Protected access modifier in Java
protected dataType variableName; protected returnType methodName(parameters) { // method body }
The protected keyword is placed before the data type or return type.
Members marked protected are accessible within the same package and by subclasses even if they are in different packages.
protected int age;
protected void showName() { System.out.println("Name shown"); }
class Parent { protected String message = "Hello"; } class Child extends Parent { void printMessage() { System.out.println(message); // can access protected member } }
This program shows a protected variable sound in the Animal class. The Dog class extends Animal and can access sound because it is protected. The makeSound method prints the sound.
class Animal { protected String sound = "Some sound"; } class Dog extends Animal { void makeSound() { System.out.println(sound); } } public class Main { public static void main(String[] args) { Dog dog = new Dog(); dog.makeSound(); } }
Protected members are not accessible by unrelated classes outside the package.
Use protected when you want to allow subclass access but keep members hidden from other classes.
Protected allows access within the same package and subclasses outside the package.
It is more open than private but more restricted than public.
Useful for sharing data or methods with child classes safely.
Practice
protected access modifier in Java?Solution
Step 1: Understand protected access scope
Theprotectedmodifier allows access to members within the same package and also to subclasses even if they are in different packages.Step 2: Compare with other access levels
Private restricts access to the same class only, public allows everywhere, and default (package-private) allows only within the same package but not subclasses outside.Final Answer:
It allows access within the same package and to subclasses outside the package. -> Option DQuick Check:
Protected = same package + subclasses outside [OK]
- Confusing protected with private
- Thinking protected is public
- Assuming protected restricts subclasses outside package
Solution
Step 1: Recall Java access modifier syntax
In Java, access modifiers come before the return type and method name. Only one access modifier is allowed per method declaration.Step 2: Identify valid syntax
protected void myMethod() {} correctly usesprotectedfollowed by the return typevoidand method name. Other options combine modifiers incorrectly or place them in wrong order.Final Answer:
protected void myMethod() {} -> Option CQuick Check:
Single access modifier before return type [OK]
- Combining multiple access modifiers
- Placing modifiers after return type
- Using invalid modifier combinations
package pkg1;
public class Parent {
protected int value = 10;
}
package pkg2;
import pkg1.Parent;
public class Child extends Parent {
public int getValue() {
return value;
}
}
public class Test {
public static void main(String[] args) {
Child c = new Child();
System.out.println(c.getValue());
}
}What is the output when running
Test.main?Solution
Step 1: Check access of protected member in subclass outside package
Thevaluefield is protected inParent. TheChildclass extendsParentand accessesvalueingetValue(). This is allowed because protected permits subclass access even outside the package.Step 2: Trace the output of
The method returnsgetValue()valuewhich is initialized to 10. So, printingc.getValue()outputs 10.Final Answer:
10 -> Option BQuick Check:
Protected allows subclass access outside package [OK]
- Thinking protected blocks subclass outside package
- Expecting compilation error
- Confusing default access with protected
package pkg1;
public class Parent {
protected int data = 5;
}
package pkg2;
import pkg1.Parent;
public class Other {
public void printData() {
Parent p = new Parent();
System.out.println(p.data);
}
}Solution
Step 1: Analyze access of protected member from non-subclass outside package
The classOtheris not a subclass ofParentand is in a different package. Accessingp.datais not allowed because protected members are accessible only within the same package or subclasses outside the package.Step 2: Identify the error type
This causes a compilation error due to illegal access of a protected member from a non-subclass outside the package.Final Answer:
Cannot access protected member from non-subclass in different package -> Option AQuick Check:
Protected blocks non-subclass outside package [OK]
- Assuming protected allows all package access
- Confusing subclass and non-subclass access
- Ignoring package differences
Animal in package zoo with a protected method makeSound(). You want to allow only subclasses in any package to use makeSound(), but prevent other classes in the same package from calling it. How can you achieve this?Solution
Step 1: Understand protected access within package and subclasses
Protected members are accessible to all classes in the same package and to subclasses outside the package. So, classes in the same package can callmakeSound()even if they are not subclasses.Step 2: Restrict access to only subclasses
To prevent other classes in the same package from accessingmakeSound(), you must move those classes to a different package. Then only subclasses (in any package) can access the protected method.Final Answer:
Declare makeSound() as protected and move other classes out of the package. -> Option AQuick Check:
Protected allows package + subclass; move non-subclass classes out [OK]
- Thinking protected blocks same-package classes
- Making other classes subclasses to restrict access
- Using private which blocks subclasses
Ready to prove it?
Pass the test and this pattern moves unknown
