Suppose a class has public visibility. In this class we define a protected method. Which of the following statements is correct?
A. This method is only accessible from inside the class itself and from inside all subclasses.
B. In a class, you cannot declare methods with a lower visibility than the visibility of the class in which it is defined.
C. From within protected methods you do not have access to public methods.
D. This method is accessible from within the class itself and from within all classes defined in the same package as the class itself.
Answer: Option D
Join The Discussion
Comments ( 4 )
What is the default access modifier for class members in Java if no access modifier is specified?
A. public
B. protected
C. private
D. package-private
A. private
B. public
C. protected
D. package-private
What keyword is used to declare a constant in Java, which is implicitly final and static?
A. final
B. const
C. static
D. constant
A. protected
B. private
C. public
D. default
Why is option D correct? We cannot access the protector methods outside of the class until and unless they are called from a object which is an instance of the sub-class of the class where protected method was defined. Right?
protected will be access same package sub class and non sub class and not access different package non sub class.but it access to object reference via different package sub class.
In the option A what do you mean "and from inside all subclasses"?
Both the options A and D are not similar?