Submitted by c-admin on Mon, 06/03/2019 - 23:57
Question:
Given the following definition of class, which member variables are accessible from OUTSIDE the package com.enthu.qb?
package com.enthu.qb;
public class TestClass{
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.
Answer and Explanation (Click to expand) Answer:
B - public things (classes, methods and fields) are accessible from anywhere.
D - protected things (methods and fields) can be accessed from within the package and from subclasses
Explanation:
public > protected > package (i.e. no modifier) > private where public is least restrictive and private is most restrictive.
Remember: 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. Note that static, final, native and synchronized are not considered as access modifiers.