Explain various Access Modifiers in Java.
Programming in Java
Computer Science Engineering
2612
William
There are two types of modifiers in Java, these are Access modifiers and non-access modifiers.
The access modifiers in Java specifies the accessibility or scope of a field, method, constructor, or class. We can change the access level of fields, constructors, methods, and class by applying the access modifier on it.
There are four types of Java access modifiers:
A Java access modifier specifies which classes can access a given class and its fields, constructors and methods. Access modifiers can be specified separately for a class, its constructors, fields and methods. Java access modifiers are also sometimes referred to in daily speech as Java access specifiers, but the correct name is Java access modifiers. Classes, fields, constructors and methods can have one of four different Java access modifiers:
Each of these Java access modifiers will be covered in the following sections of this Java access modifier tutorial. The following table summarizes what Java constructs each Java access modifier can be applied to:
If a method or variable is marked as& private
(has the code>private access modifier assigned to it), then only code inside the same class can access the variable, or call the method. Code inside subclasses cannot access the variable or method, nor can code from any external class.
There is an example of assigning the private
access modifier to a field:
public class Clock { private long time = 0; }
In some cases the fields are truly private, meaning they are only used internally in the class. In other cases the fields can be accessed via accessor methods. In the above example the two methods getTime()
and setTime()
can access the time
member variable.
public class Clock { private long time = 0; public long getTime() { return this.time; } public void setTime(long theTime) { this.time = theTime; } }
If a constructor in a class is assigned the private
Java access modifier, that means that the constructor cannot be called from anywhere outside the class. Here is a Java class example of private constructor.
public class Clock { private long time = 0; private Clock(long time) { this.time = time; } public Clock(long time, long timeOffset) { this(time); this.time += timeOffset; } public static Clock newClock() { return new Clock(System.currentTimeMillis()); } }
This version of the Clock
class contains a private
constructor and a public
constructor. The private
constructor is called from the public constructor (the statement this();
). The private
constructor is also called from the static
method newClock()
.
The default Java access modifier is declared by not writing any access modifier at all. The default access modifier means that code inside the class itself as well as code inside classes in the same package as this class. Therefore, the default
access modifier is also sometimes referred to as the package
access modifier. Here is an default / package access modifier example. In given example the subclass SmartClock
has a method called getTimeInSeconds()
which accesses the time
variable of the superclass Clock
public class Clock { long time = 0; } public class ClockReader { Clock clock = new Clock(); public long readClock{ return clock.time; } }
The protected access modifier provides the same access as the default access modifier, with the addition that subclasses can access protected methods and member variables (fields) of the superclass.
Here is a protected
access modifier example:
public class Clock { protected long time = 0; // time in milliseconds } public class SmartClock() extends Clock{ public long getTimeInSeconds() { return this.time / 1000; } }
The Java access modifier public
means that all code can access the class, field, constructor or method, regardless of where the accessing code is located. The accessing code can be in a different class and different package. Here is a public
access modifier example:
public class Clock { public long time = 0; } public class ClockReader { Clock clock = new Clock(); public long readClock{ return clock.time; } }
The Java access modifiers private
and protected
cannot be assigned to a class. Only to constructors, methods and fields inside classes. Classes can only have the default (package) and public
access modifier assigned to them. If the class is marked with the default
access modifier, then no other class outside the same Java package can access that class, including its constructors, fields and methods. It doesn't help that you declare these fields public
, or even public static
. It is important to keep in mind that the Java access modifier assigned to a Java class takes precedence over any access modifiers assigned to fields, constructors and methods of that class.
Java interfaces are meant to specify fields and methods that are publicly available in classes that implement the interfaces. Therefore you cannot use the private
and protected
access modifiers in interfaces. Fields and methods in interfaces are implicitly declared public
if you leave out an access modifier, so you cannot use the default access modifier either (no access modifier).