1
1

A method within a class is only accessible by classes that are defined within the same package as the class of the method. Which one of the following is used to enforce such restriction?

A method within a class is only accessible by classes that are defined within the same package as the class of the method. Which one of the following is used to enforce such restriction?

A).  Declare the method with the keyword public.

B).  Declare the method with the keyword private.

C).  Declare the method with the keyword protected.

D).  Do not declare the method with any accessibility modifiers.

E).  Declare the method with the keyword public and private.

This Question has 1 answers.

 Do not declare the method with any accessibility modifiers.

  1. In Java, if a method is declared without any access modifier, it has package-private (default) access.
  2. This means the method is accessible only within the same package but not from outside the package, even by subclasses.
  3. Other options:
    A) public → Accessible from anywhere.
    B) private → Accessible only within the same class.
    C) protected → Accessible within the same package and subclasses.
    E) public and private → This is not possible in Java.
Thus, leaving out the access modifier (default access) restricts method access to classes within the same package.

Add Answer / Comment

Captcha Image