Examveda
Examveda

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


This Question Belongs to Java Program >> Declaration And Access Control

Join The Discussion

Comments ( 4 )

  1. Konark Lohat
    Konark Lohat :
    8 months ago

    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?

  2. NITHU NITHYA
    NITHU NITHYA :
    5 years ago

    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.

  3. Mohammed Amine
    Mohammed Amine :
    6 years ago

    In the option A what do you mean "and from inside all subclasses"?

  4. Balaji Srikaanth
    Balaji Srikaanth :
    7 years ago

    Both the options A and D are not similar?

Related Questions on Declaration and Access Control