Java Access Modifiers
Java Access Modifiers
Java classes, fields, constructors and methods can have one of four different access modifiers:
- private
- package (default)
- protected
- public
private
If a method or variable is marked as
private
, 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.
If a class is marked as private then no external class an access the class. This doesn't really make so much sense for classes though. Therefore, the access modifier
private
is mostly used for fields, constructors and methods.
Here is an example of a
private
field:public class Clock { private long time = 0; }
The member variable
time
inside the Clock
class cannot be accessed from code outside the Clock
class. But code inside the class can access the time
variable. For instance:public class Clock { private long time = 0; public long getTime() { return this.time; } public void setTime(long theTime) { this.time = theTime; } }
In the above example the two methods
getTime()
and setTime()
can access the time
member variable. The two methods are declared public, meaning they can be called from code anywhere in your application.package (default)
The default access level is declared by not writing any access modifier at all. Default access levels means that code inside the class itself + code inside classes in the same package as this class, can access the class, field, constructor or method. Therefore, the
default
access modifier is also sometimes called apackage
access modifier.
Subclasses cannot access methods and member variables in the superclass, if they have default accessibility declared, unless the subclass is located in the same package as the superclass.
Here is an example:
public class Clock { long time = 0; } public class ClockReader { Clock clock = new Clock(); public long readClock{ return clock.time; } }
The
ClockReader
class above can read the time
member variable of the Clock
object, provided thatClockReader
and Clock
are located in the same package.protected
The
protected
acces modifier does the same as the default
access, except subclasses can also access protected methods and member variables of the superclass. This is true even if the subclass is not located in the same package as the superclass.
Here is an 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.public
The
public
access modifier means that all code can access the class, field, constructor or method, regardless of where the accessing code is located.
Here is a public field example:
public class Clock { public long time = 0; } public class ClockReader { Clock clock = new Clock(); public long readClock{ return clock.time; } }
The
ClockReader
class can access the time
field in the Clock
no matter what package theClockReader
is located in, because both the Clock
class and the time
field are declared public
0 comments: