0
0
Javaprogramming~15 mins

Protected access modifier in Java - Cheat Sheet & Quick Revision

Choose your learning style8 modes available
overviewRecall & Review
beginner
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.
touch_appClick 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.
touch_appClick 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.
touch_appClick 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.
touch_appClick 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.
touch_appClick 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
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
What is the default access level if no modifier is specified in Java?
Apublic
Bprivate
Cprotected
Dpackage-private (default)
Which access modifier allows the widest access?
Apublic
Bprivate
Cprotected
Ddefault
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
Explain in your own words what the protected access modifier does in Java.
Describe a real-life example that helps you remember when to use protected in Java.