Access Modifier Specifier
Access Modifier Specifier
Access modifiers specifies who can access them. There are four access modifiers used in java. They are public, private, protected, no modifer (declaring without an access modifer). Using no modifier is also sometimes referred as package-private or default or friendly access. Usage of these access modifiers is restricted to two levels. The two levels are class level access modifiers and member level access modifiers.
II) Member level access modifiers (java variables and java methods)
All the four public, private, protected and no modifer is allowed. public and no modifier the same way as used in class level. private members CAN ONLY access. protected CAN be accessed from same package and a subclass existing in any package can access. For better understanding, member level access is formulated as a table:
Access Modifiers
public protected no access modifier default private
Same Class Y Y Y Y
Same Package Y Y Y N
Subclass Y Y N N
Other packages Y N N N
First row {public Y Y Y Y} should be interpreted as: Y A member declared with public access modifier CAN be accessed by the members of the same class. Y A member declared with public access modifier CAN be accessed by the members of the same package. Y A member declared with public access modifier CAN be accessed by the members of the subclass. Y A member declared as public CAN be accessed from Other packages. Second row {protected Y Y Y N} should be interpreted as: Y A member declared with protected access modifier CAN be accessed by the members of the same class. Y A member declared with protected access modifier CAN be accessed by the members of the same package. Y A member declared with protected access modifier CAN be accessed by the members of the subclass. N A member declared with protected access modifier CANNOT be accessed by the members of the Other package. similarly interpret the access modifiers table for the third (no access modifier) and fourth (private access modifier) records.
Class Y Y Y Y
Package Y Y Y N
Subclass Y Y N N
World Y N N N
Access Levels The first data column indicates whether the class itself has access to the member defined by the access level. As you can see, a class always has access to its own members. The second column indicates whether classes in the same package as the class (regardless of their parentage) have access to the member. The third column indicates whether subclasses of the class declared outside this package have access to the member. The fourth column indicates whether all classes have access to the member. Access levels affect you in two ways. First, when you use classes that come from another source, such as the classes in the Java platform, access levels determine which members of those classes your own classes can use. Second, when you write a class, you need to decide what access level every member variable and every method in your class should have. Let's look at a collection of classes and see how access levels affect visibility. The following figure shows the four classes in this example and how they are related.
Classes and Packages of the Example Used to Illustrate Access Levels The following table shows where the members of the Alpha class are visible for each of the access modifiers that can be applied to them. Modifier public protected no modifier private Visibility Tips on Choosing an Access Level:
If other programmers use your class, you want to ensure that errors from misuse cannot happen. Access levels can help you do this. o Use the most restrictive access level that makes sense for a particular member. Use private unless you have a good reason not to. o Avoid public fields except for constants. (Many of the examples in the tutorial use public fields. This may help to illustrate some points concisely, but is not recommended for production code.) Public fields tend to link you to a particular implementation and limit your flexibility in changing your code.
Alpha Y Y Y Y
Beta Y Y Y N
Alphasub Y Y N N
Gamma Y N N N