What does the protected access modifier mean in Java?
It means the member is accessible within its own package and by subclasses even if they are in different packages.
Click to reveal answer
beginner
Can a protected member be accessed by classes in a different package that are not subclasses?
No, only subclasses or classes in the same package can access protected members.
Click to reveal answer
intermediate
Which of these access modifiers allows access only within the same package and subclasses outside the package? public, private, protected, default
protected allows access within the same package and to subclasses outside the package.
Click to reveal answer
beginner
Example: If class Animal has a protected method makeSound(), can a subclass Dog in a different package call makeSound()?
Yes, because Dog is a subclass, it can access the protected method even if it is in a different package.
Click to reveal answer
beginner
True or False: <code>protected</code> members are accessible from any class in the Java program.
False. protected members are not accessible from all classes, only from the same package or subclasses.
Click to reveal answer
Which classes can access a protected member in Java?
AOnly classes in the same package and subclasses
BAny class in any package
COnly classes in the same package
DOnly subclasses in the same package
✗ Incorrect
protected members are accessible within the same package and also by subclasses outside the package.
If a class Car has a protected field, can a non-subclass in a different package access it?
AYes, always
BOnly if it is in the same package
CNo, never
DYes, if it imports the class
✗ Incorrect
Only subclasses or classes in the same package can access protected members, not unrelated classes in other packages.
What is the default access level if no modifier is specified in Java?
Apublic
Bprivate
Cprotected
Dpackage-private (default)
✗ Incorrect
When no modifier is given, the member is accessible only within the same package, called package-private or default access.
Which access modifier allows the widest access?
Apublic
Bprivate
Cprotected
Ddefault
✗ Incorrect
public allows access from any class anywhere.
Can a subclass in a different package access a protected member of its superclass?
ANo
BYes
COnly if the member is static
DOnly if the subclass is final
✗ Incorrect
Subclasses can access protected members of their superclass even if they are in different packages.
Explain in your own words what the protected access modifier does in Java.
Think about who can see the member: friends in the same neighborhood and family members even if they live far.
You got /3 concepts.
Describe a real-life example that helps you remember when to use protected in Java.
Imagine a secret recipe shared only with family and neighbors.
You got /3 concepts.
Practice
(1/5)
1. Which statement best describes the protected access modifier in Java?
easy
A. It allows access only within the same class.
B. It allows access only within the same package, not subclasses outside.
C. It allows access from any class anywhere.
D. It allows access within the same package and to subclasses outside the package.
Solution
Step 1: Understand protected access scope
The protected modifier 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 D
Quick Check:
Protected = same package + subclasses outside [OK]
Hint: Protected means package + subclass access [OK]
2. Which of the following is the correct way to declare a protected method in Java?
easy
A. private protected void myMethod() {}
B. public protected void myMethod() {}
C. protected void myMethod() {}
D. void protected myMethod() {}
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 uses protected followed by the return type void and method name. Other options combine modifiers incorrectly or place them in wrong order.
Final Answer:
protected void myMethod() {} -> Option C
Quick Check:
Single access modifier before return type [OK]
Hint: Use only one access modifier before return type [OK]
Common Mistakes:
Combining multiple access modifiers
Placing modifiers after return type
Using invalid modifier combinations
3. Consider the following code in two packages:
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?
medium
A. 0
B. 10
C. Compilation error due to access
D. Runtime error
Solution
Step 1: Check access of protected member in subclass outside package
The value field is protected in Parent. The Child class extends Parent and accesses value in getValue(). This is allowed because protected permits subclass access even outside the package.
Step 2: Trace the output of getValue()
The method returns value which is initialized to 10. So, printing c.getValue() outputs 10.
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);
}
}
medium
A. Cannot access protected member from non-subclass in different package
B. No error, code runs fine
C. Syntax error in import statement
D. Cannot create instance of Parent class
Solution
Step 1: Analyze access of protected member from non-subclass outside package
The class Other is not a subclass of Parent and is in a different package. Accessing p.data is 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 A
5. You have a class 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?
hard
A. Declare makeSound() as protected and move other classes out of the package.
B. Declare makeSound() as protected and make other classes subclasses of Animal.
C. Declare makeSound() as private and provide a public wrapper method.
D. Declare makeSound() as protected and keep other classes in the same package.
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 call makeSound() even if they are not subclasses.
Step 2: Restrict access to only subclasses
To prevent other classes in the same package from accessing makeSound(), 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 A
Quick Check:
Protected allows package + subclass; move non-subclass classes out [OK]
Hint: Protected allows package access; move classes to restrict [OK]
Common Mistakes:
Thinking protected blocks same-package classes
Making other classes subclasses to restrict access
Using private which blocks subclasses
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.