Java OCA OCP Practice Question 1702

Question

Given the following declaration of a class, which fields are accessible from outside the package com.corporation.project?

package com.corporation.project;

public class MyClass {
            int i;
  public    int j;
  protected int k;
  private   int l;
}

Select the two correct answers.

  • (a) Field i is accessible in all classes in other packages.
  • (b) Field j is accessible in all classes in other packages.
  • (c) Field k is accessible in all classes in other packages.
  • (d) Field k is accessible in subclasses only in other packages.
  • (e) Field l is accessible in all classes in other packages.
  • (f) Field l is accessible in subclasses only in other packages.


(b) and (d)

Note

Outside the package, the member j is accessible to any class, whereas the member k is only accessible to subclasses of MyClass.

The field i has package accessibility, and is only accessible by classes inside the package.

The field j has public accessibility, and is accessible from anywhere.

The field k has protected accessibility, and is accessible from any class inside the package and from subclasses anywhere.

The field l has private accessibility, and is only accessible within its own class.




PreviousNext

Related