Access Modifiers in java
Access Modifiers in Java
Access to classes is
controlled using Access Modifiers aka ( Access Specifiers, Visibility
Specifiers) in java. These are used to regulate access to classes, fields and
methods in java. Using access modifiers you can control the access of
variables, methods with in a class. Java provides number of access modifiers to
control the access for class as well as the fields, methods and constructors in
your classes.
We have 4 types of access
modifiers in java which are explained below:-
Default: Visible
to the package, the default. No modifiers are needed, no keyword is required.
Private:
Visible to the class only.
Public: Visible
to the world.
Protected: Visible
to the package and all subclasses.
Default: We
do not have any default specifier in Java. It provides Default specifier which
is used where no access modifier is present. Using this you can access class,
methods or fields which belongs to same package, but not from outside the
package. Example
Class demo
{
Int a;
}
Private: Private
access modifier is being accessed within the same class to which the methods
and fields belong. It is most
restrictive access modifier in java. Private access modifiers cannot be
accessed outside the class or classes inheriting that class or within
subclasses. Which shows that private modifier is just opposite to public access
modifier. Using the private modifier is the main way that an object encapsulates
itself and hides data from the outside world. Example
Class demo
{
Private int a;
}
Public: It
has the widest scope among all other modifiers and having highest level of
accessibility. Methods, fields and constructors declared as public within public
class are visible to any class, weather these classes are in same package or in any other package. If the public class we are trying to access is
in a different package, then the public class still needs to be imported. Example
Class demo
{
public x,y;
public static void
main(String[] args){
}
}
The main() method needs to be
public to be accessed by java interpreter to run the class.
Protected: Methods, fields and constructors declared
protected in a superclass can be accessed only by subclasses in other packages.
Protected access modifier is accessible within same package and outside the
package but through inheritance only. Protected access modifier cannot be
applied to class and interfaces where as methods, fields can be declared
protected, however methods and fields in interface cannot be declared
protected. Example
Class demo
{
protected boolean openBook();
}
The following rules for inherited methods are enforced
−
·
Methods declared
public in a superclass also must be public in all subclasses.
· Methods declared
protected in a superclass must either be protected or public in subclasses;
they cannot be private.
·
Methods declared
private are not inherited at all, so there is no rule for them.
Access Modifier
|
within class
|
within package
|
outside package by subclass only
|
Private
|
Y
|
N
|
N
|
Default
|
Y
|
Y
|
N
|
Protected
|
Y
|
Y
|
Y
|
Public
|
Y
|
Y
|
Y
|
Comments
Post a Comment