Java OCA OCP Practice Question 1392

Question

Given the following definition of class, which member variables are accessible from OUTSIDE the package com.mypkg.mypkg2?

package com.mypkg.mypkg2; 
public class Main{ 
   int i; 
   public int j; 
   protected int k; 
   private int l; 
} 

Select 2 options

  • A. Member variable i.
  • B. Member variable j .
  • C. Member variable k.
  • D. Member variable k, but only for subclasses.
  • E. Member variable l.


Correct Options are  : B D

Note

public > protected > package (i.e. no modifier) > private

where public is least restrictive and private is most restrictive.

protected is less restrictive than package access.

So a method or field declared as protected will be accessible from a subclass even if the subclass is not in the same package.

The same is not true for package access.

A top level class can only have either public or no access modifier but a method or field can have all the four.

static, final, native and synchronized are not considered as access modifiers.




PreviousNext

Related