Access Modifier
Now you surely would have noticed by now the words
public, protected and private at the beginning of class's method declarations
used in this book. These keywords are called the access modifiers in the Java
language syntax, and can be defined as..
"Keywords that help set the visibility and accessibility
of a class, its member variables, and methods is called Access Modifier"
Access modifiers specify who can access them. There are four access
modifiers used in java. They are public, private, protected, no modifier
(declaring without an access modifier). 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:
Class level access modifiers : Only two access modifiers is allowed,
public and no modifier. If a class is 'public', then it can be accessed from
anywhere. If a class has "no modifier", then it can
only be accessed from "same" package'.
Member level access modifiers : All the four public, private, protected
and no modifier 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.
The Following Table shows what Access Modifiers are appropriate for classes,
nested classes, member variables, and methods:
|
Class |
Nested class |
Method Method variable |
Interface |
Interface
method signature |
public |
visible from anywhere |
same as its class |
same as its class |
visible from anywhere |
visible from anywhere |
protected |
N/A |
its class and it's sub
class |
its class and it's sub
class |
N/A |
N/A |
package |
only from it's
package |
only From it's
package |
only from its
package |
N/A |
N/A, default
package |
default |
N/A |
only from it's class |
only from it's class |
N/A |
N/A |
Public Key Word:
Public is a Java keyword which declares a member's access as public. Public
members are visible to all other classes. This means that any other class can
access a public field or method. Further, other classes can modify public fields
unless the field is declared as final.A best practice is to give fields private
access and reserve public access to only the set of methods and final fields
that define the class' public constants. This helps with encapsulation and
information hiding, since it allows you to change the implementation of a class
without affecting the consumers who use only the public API of the class.
Protected Key Word:
Protected is a Java keyword. This keyword is an access modifier, used before a
method or other class member to signify that the method or variable can only be
accessed by elements residing in its class, subclasses, or classes in the same
package.
Syntax:
protected <return Type> <method Name>(<parameters>);
For example:
protected int getAge();
protected void setYearOfBirth(int year);
Private Key Word: Private is a Java keyword which declares a member's
access as private. That is, the member is only visible within the class, not
from any class. The visibility of private members extends to nested classes.
Syntax:
private void method();
Default Access Modifier - No keyword:Default access modifier means we do not
explicitly declare an access modifier for a class, field, method etc.A variable
or method declared without any access control modifier is available to any other
class in the same package. The default modifier cannot be used for methods,
fields in an interface.
Example:
Variables and methods can be declared without any modifiers, as in the following
examples:
String version = "1.5.1";
boolean processOrder()
{
return true;
}
Some Example Of Uses Of these Access Specifier:
A.java:
class Exp
{
private String name;
protected void set(String nm)
{
name = nm;
}
public Exp(String name)
{
this.name = name;
}
public String toString()
{
return "I'm a vikas and my name is " + name;
}
}
public class A extends
Exp
{
private int aNumber;
public A(String name, int aNumber)
{
super(name);
this.aNumber = aNumber;
}
public void change(String name, int aNumber)
{
set(name);
this.aNumber = aNumber;
}
public String toString()
{
return "A" + aNumber + ": "
+ super.toString();
}
public static void main(String[] args)
{
A a = new A("vikky",
12);
System.out.println(a);
a.change("mishra", 19);
System.out.println(a);
}
}
Output: A.java:
My1.java:
class MySingleton
{
private static MySingleton theObject;
private MySingleton()
{
}
public static MySingleton
createMySingleton()
{
if (theObject == null)
theObject = new MySingleton();
return theObject;
}
}
public class My1
{
public static void main(String[] args)
{
MySingleton ms1 = MySingleton.createMySingleton();
MySingleton ms2 = MySingleton.createMySingleton();
System.out.println(ms1 == ms2);
}
}
Output: