Access Modifiers in Java
Sourabrata Mukherjee
Topics:
● Why Access Modifiers?
● Private
● Default (package)
● Protected
● Public
● Access Modifiers and Inheritance
● Access Modifiers on Local Variables
Why Access Modifiers?
The use of modifiers goes to the core concepts of encapsulation, aka 'data hiding'
in object-oriented development. Variables should never be public. That is the
whole point of private/protected modifiers on them: to prevent direct access to
the variables themselves. You provide methods to manipulate variables. A method
has to be public in order to allow other programmers (or classes) access to that
data. But by using methods, you can control how a variable is manipulated. Which
is the entire point because you've hidden the details of the variable behind the
method.
Continue..
Let Suppose you have class which contains two variable age and name. Now you
have created getter setter for these variable. It is obvious that variable which we
declared we will mark it as private, Why because i don't want to give the
accessibility of that variable to other class directly. Now The question is why i
don't want to give accessibility directly to other class?. The Answer is because i
want to control my variable. If i will give direct accessibility to other class, Other
class can assign age as negative which is not good, age cannot be a negative
value. Hence to control this thing i am exposing getter and setter method.
Continue..
Other class object will call my setter method and supply value. Inside method i can
control the value like if age is negative simply assign 0 or if age is more than 200
throw exception saying age cannot be 200. Now what is another class object want
to read data from variable. For that i have given one getter method which would be
public in nature. Since my variable is private hence other class object cannot
directly access my variable hence this getter method is required which will simply
return the variable. Access modifier are used to control the visibility of any
variable or method and this is necessary .
Private
The private access modifier is accessible only within class.
A class cannot be private or protected except nested class.
If you make any class constructor private, you cannot create the instance of that
class from outside the class.
Continue..
private Constructors:
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. A
private constructor can still get called from other constructors, or from static
methods in the same class. Here is an example,
Continue..
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());
}
Continue..
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 above example only serves to show you that a private constructor
can be called from public constructors and from static methods inside the same
class. Do not perceive the above example as an example of clever design in any
way.
Default
If you don't use any modifier, it is treated as default by default. The default
modifier is accessible only within package.
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, can access the class, field,
constructor or method which the default access modifier is assigned to.
Therefore, the default access modifier is also sometimes referred to as the
package access modifier.
Protected
The protected access modifier is accessible within package and outside the
package but through inheritance only. The protected access modifier can be
applied on the data member, method and constructor. It can't be applied on the
class.
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. This is true even if the subclass is not
located in the same package as the superclass.
Public
The public access modifier is accessible everywhere. It has the widest scope
among all other modifiers.
The accessing code can be in a different class and different package.
Access Modifiers and Inheritance
When you create a subclass of some class, the methods in the subclass cannot
have less accessible access modifiers assigned to them than they had in the
superclass. For instance, if a method in the superclass is public then it must be
public in the subclass too, in case the subclass overrides the method. If a method
in the superclass is protected then it must be either protected or public in the
subclass. While it is not allowed to decrease accessibility of an overridden
method, it is allowed to expand accessibility of an overridden method. For
instance, if a method is assigned the default access modifier in the superclass,
then it is allowed to assign the overridden method in the subclass the public
access modifier.
Access Modifiers on Local Variables
No Access Modifiers can be applied to local variables. Only final can be applied to
a local variable which is a Non Access Modifier .
Conclusion
Thank You

Access modifiers in java

  • 1.
    Access Modifiers inJava Sourabrata Mukherjee
  • 2.
    Topics: ● Why AccessModifiers? ● Private ● Default (package) ● Protected ● Public ● Access Modifiers and Inheritance ● Access Modifiers on Local Variables
  • 3.
    Why Access Modifiers? Theuse of modifiers goes to the core concepts of encapsulation, aka 'data hiding' in object-oriented development. Variables should never be public. That is the whole point of private/protected modifiers on them: to prevent direct access to the variables themselves. You provide methods to manipulate variables. A method has to be public in order to allow other programmers (or classes) access to that data. But by using methods, you can control how a variable is manipulated. Which is the entire point because you've hidden the details of the variable behind the method.
  • 4.
    Continue.. Let Suppose youhave class which contains two variable age and name. Now you have created getter setter for these variable. It is obvious that variable which we declared we will mark it as private, Why because i don't want to give the accessibility of that variable to other class directly. Now The question is why i don't want to give accessibility directly to other class?. The Answer is because i want to control my variable. If i will give direct accessibility to other class, Other class can assign age as negative which is not good, age cannot be a negative value. Hence to control this thing i am exposing getter and setter method.
  • 5.
    Continue.. Other class objectwill call my setter method and supply value. Inside method i can control the value like if age is negative simply assign 0 or if age is more than 200 throw exception saying age cannot be 200. Now what is another class object want to read data from variable. For that i have given one getter method which would be public in nature. Since my variable is private hence other class object cannot directly access my variable hence this getter method is required which will simply return the variable. Access modifier are used to control the visibility of any variable or method and this is necessary .
  • 6.
    Private The private accessmodifier is accessible only within class. A class cannot be private or protected except nested class. If you make any class constructor private, you cannot create the instance of that class from outside the class.
  • 7.
    Continue.. private Constructors: If aconstructor in a class is assigned the private Java access modifier, that means that the constructor cannot be called from anywhere outside the class. A private constructor can still get called from other constructors, or from static methods in the same class. Here is an example,
  • 8.
    Continue.. 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()); }
  • 9.
    Continue.. This version ofthe 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 above example only serves to show you that a private constructor can be called from public constructors and from static methods inside the same class. Do not perceive the above example as an example of clever design in any way.
  • 10.
    Default If you don'tuse any modifier, it is treated as default by default. The default modifier is accessible only within package. 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, can access the class, field, constructor or method which the default access modifier is assigned to. Therefore, the default access modifier is also sometimes referred to as the package access modifier.
  • 11.
    Protected The protected accessmodifier is accessible within package and outside the package but through inheritance only. The protected access modifier can be applied on the data member, method and constructor. It can't be applied on the class. 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. This is true even if the subclass is not located in the same package as the superclass.
  • 12.
    Public The public accessmodifier is accessible everywhere. It has the widest scope among all other modifiers. The accessing code can be in a different class and different package.
  • 13.
    Access Modifiers andInheritance When you create a subclass of some class, the methods in the subclass cannot have less accessible access modifiers assigned to them than they had in the superclass. For instance, if a method in the superclass is public then it must be public in the subclass too, in case the subclass overrides the method. If a method in the superclass is protected then it must be either protected or public in the subclass. While it is not allowed to decrease accessibility of an overridden method, it is allowed to expand accessibility of an overridden method. For instance, if a method is assigned the default access modifier in the superclass, then it is allowed to assign the overridden method in the subclass the public access modifier.
  • 14.
    Access Modifiers onLocal Variables No Access Modifiers can be applied to local variables. Only final can be applied to a local variable which is a Non Access Modifier .
  • 15.
  • 16.