Java Access Modifiers

A Java access modifier specifies which classes can access a given class and its fields, constructors and methods. There are four types of access modifiers in Java there are as follows: 1.private 2.default (package) 3.protected 4.public

Java Access Modifiers - private keyword If a class member is “private” then it will be accessible only inside the same class. This is the most restricted access and the class member will not be visible to the outer world. Usually, we keep class variables as private and methods that are intended to be used only inside the class as private. Example: public class Clock { private long time = 0; } The member variable time has been marked as private. That means, that the member variable time inside the Clock class cannot be accessed from code outside the Clock class.

Java Access Modifiers - default access If a class member doesn’t have any access modifier specified, then it’s treated with default access. The access rules are similar to classes and the class member with default access will be accessible to the classes in the same package only. This access is more restricted than public and protected but less restricted than private. (Least Accessible) private < default < protected < public (Most Accessible) Example:

public class Clock { long time = 0; }

public class ClockReader { Clock clock = new Clock();

public long readClock{ return clock.time; } } The time field in the Clock class has no access modifier, which means that it is implicitly assigned the default / package access modifier. Therefore, the ClockReader class can read the time member variable of the Clock object, provided that ClockReader and Clock are located in the same Java package.

Java Access Modifiers - protected keyword If class member is “protected” then it will be accessible only to the classes in the same package and to the subclasses. This modifier is less restricted from private but more restricted from public access. Usually, we use this keyword to make sure the class variables are accessible only to the subclasses. Example: public class Clock { protected long time = 0; // time in milliseconds }

public class SmartClock() extends Clock{

public long getTimeInSeconds() { return this.time / 1000; } } In the above example the subclass SmartClock has a method called getTimeInSeconds() which accesses the time variable of the superclass Clock. This is possible even if Clock and SmartClock are not located in the same package, because the time field is marked with the protected Java access modifier.

Java Access Modifiers - public keyword If a class member is “public” then it can be accessed from anywhere. The member variable or method is accessed globally. This is the simplest way to provide access to class members. However, we should take care of using this keyword with class variables otherwise anybody can change the values. Usually, class variables are kept as private and getter-setter methods are provided to work with them. Example: public class Clock { public long time = 0; }

public class ClockReader { Clock clock = new Clock();

public long readClock{ return clock.time; } } The time field in the Clock class is marked with the public Java access modifier. Therefore, the ClockReader class can access the time field in the Clock no matter what package the ClockReader is located in.