There
are two types of modifiers in Java:
1.
Access modifiers
2.
Non-access modifiers
1.
Access Modifiers
The
Access
modifiers in
Java
programming
language specifies the accessibility
/ scope
of
a field,
class,
method
or constructor.
We can change the access
level of
field,
class,
method
or constructor
by applying the access
modifier on
it.
In
other words, the access
modifiers in
Java
programming
language helps to restrict
the
scope
of
a field,
class,
variable,
method
or constructor.
There
are four types of access
modifiers available
in Java:
1.
Default access modifier: no keyword required
2.
Public access modifier
3.
Private access modifier
4.
Protected access modifier
1.
Default
The
access level of a default modifier is only within the package. It
cannot be accessed from outside of the package. If you do not specify
any access level, it will be the default. It provides more
accessibility than private. But it is more restrictive than protected
and public.
The
default modifier is applicable for class, methods, variables, enums,
constructors, interfaces
Example:
Java default modifier demo
package
com.as;
class
Abc{
void
m1{
System.out.println
(“Ankur Saxena Blogs!”);
}
}
class
A{
public
static void main (String[] args){
Abc
t = new Abc ();
t.m1();
}
}
Save
this file as ‘Abc.java’
Compile:
$javac –d . Abc.java
Execute:
$ java
com.as.A
Note:
where
–d
represents
current
directory.
Output:
Ankur
Saxena Blogs!
2.
Public
The
access level of a public modifier is everywhere. It can be accessed
from within the class, outside of the class, within the package,
outside of the package. It has the widest scope among all over access
modifiers. It is the most accessible modifier in Java programming
language.
Public
modifier:
all
access
Example:
Java public modifier demo
public
class Abc1{
public
void m1(){
System.out.println
("Ankur Saxena Blogs!");
}
}
class
Bcd{
public
static void main (String[] args){
//creating
new object
Abc1
t = new Abc1 ();
t.m1();
}
}
Save
this file as ‘Abc1.java’
Compile:
$javac Abc1.java
Execute:
$java Bcd
Output:
Ankur
Saxena Blogs!
3.
Private
The
access level of a private modifier is only within the class. It
cannot be accessed from outside of the class. It is the most
restricted modifier in Java programming language.
Private
modifier is applicable for variables, methods and constructors. We
can access the private modifier only within the same class.
Example:
Java private modifier demo
class
Tcs{
private
int num = 10;
}
class
Tcs1 extends Tcs{
void
m1 (){
System.out.println
("The number is :: "+num);
}
public
static void main (String[] args){
//creating
new object
Tcs1
t = new Tcs1 ();
t.m1();
}
}
Save
this file as ‘Tcs.java’
Compile:
$javac Tcs.java
Output:
Tcs.java:6:
error:
num has private access in Tcs
System.out.println
("The number is :: "+num);
^
1
error
4.
Protected
The
access level of a protected modifier is within the package and
outside of the package through the child class / inheritance only. If
you do not make the child class, it cannot be accessed from outside
of the package.
Protected
modifier is applicable for variables, methods and constructors
Example:
Java protected modifier demo
//Java
program to illustrate protected modifier
//package
p1
package
p1;
public
class Test1{
protected
void m1(){
System.out.println
("Ankur Saxena Blogs!");
}
}
//package
p2
package
p2;
//import
all classes in package p1
import
p1.*;
//class
Test2 is a subclass of Test1
class
Test2 extends Test1{
public
static void main (String[] args){
//creating
new object
Test2
t = new Test2();
//calling
m1 method
t.m1();
}
}
Save
this file as ‘Test1.java’
Compile:
$javac Test1.java
Execute:
$java Test1
Output:
Java
Access Modifiers: Access Level Table
Modifiers class method constructor variable
Public yes yes yes yes
Default yes yes yes yes
Private no yes yes yes
Protected no yes yes yes
Java
Access Modifiers: Permission
Public Default Private Protected
within
the class yes yes yes yes
same
pkg sub class yes yes no yes
same
pkg non-sub class yes yes no yes
diff.
pkg sub class yes no no yes
diff.
pkg non-sub class yes no no no
0 Comments