2. Presented by,
Name :MD.Rakib Hossen
ID :212902020
Section :PC-212(DA)
Department: BSc.in CSE in GUB
Presented To,
DR. MUHAMMAD AMINUR RAHAMAN
Associate Professor and Campus Director,
Permanent Campus in GUB
02
3. What Are Access Modifiers?
In java , Access modifiers
are used to set the
accessibility (visibility) of
classes, interfaces, variables,
methods.
03
4. Type of Access Modifiers
Four type of access modifiers,
1. Default
2. Public
3. Protected
4. Private
04
5. Default Access Modifiers
A default access modifier in Java has no specific keyword.
Whenever the access modifier is not specified, then it is
assumed to be the default. The entities like classes, methods,
and variables can have a default access.
A default class is accessible inside the package but it is not
accessible from outside the package i.e. all the classes inside the
package in which the default class is defined can access this
class.
05
6. Default Access Modifiers
For example,
void math4(){
result = a+b-c;
System.out.println("Result by defualt modifier= "+result);
}
t1.math4(); //try access defualt modifiers in same class. successfully access
t2.math4(); //try access defualt modifiers in same pacakge sub class. successfully access
t3.math4(); //try access defualt modifiers in same pacakge non-sub class. successfully access
//t4.mathy4; //try access defualt modifiers in Different pacakge non-sub class. not
successfully access
//t5.mathy4; //try access defualt modifiers in Different pacakge sub class. not successfully
access
06
7. Public Access Modifiers
A class or a method or a data field specified as
‘public’ is accessible from any class or package in
the Java program. The public entity is accessible
within the package as well as outside the
package. In general, public access modifier is a
modifier that does not restrict the entity at all.
07
8. Public Access Modifiers
public void math1(){
result= a+b-c;
System.out.println("Result print by public modifier= "+result);
}
t1.math1(); // try access public modifiers in same class. successfully access
t2.math1(); // try access public modifiers in same pacakge sub class. successfully access
t3.math1(); // try access public modifiers in same pacakge non-sub class. successfully
access
t4.math1(); // try access public modifiers in Different pacakge non-sub class.
successfully access
t5.math1(); // try access public modifiers in Different pacakge sub class. successfully
access
08
9. Protected Access Modifiers
The protected access modifiers allows access to
entities through subclasses of the class in which the
entity is declared. It doesn’t matter whether the class
is in the same package or different package, but as
long as the class that is trying to access a protected
entity is a subclass of this class, the entity is
accessible.
09
10. Protected Access Modifiers
protected void math3(){
result= a+b-c;
System.out.println("Result by protected modifier= "+result);
}
t1.math3(); //try access protected modifiers in same class. successfully access
t2.math3(); //try access protected modifiers in same pacakge sub class. successfully
access
t3.math3(); //try access protected modifiers in same pacakge non-sub class.
successfully access
// t4.math3(); //try access protected modifiers in Different pacakge non-sub class. not
successfully access
t5.math3(); //try access protected modifiers in Different pacakge sub class.
successfully access
10
11. Private Access Modifiers
The ‘private’ access modifier is the one that has the
lowest accessibility level. The methods and fields that
are declared as private are not accessible outside the
class. They are accessible only within the class which
has these private entities as its members.
Note that the private entities are not even visible to
the subclasses of the class. A private access modifier
ensures encapsulation in Java.
11
12. Private Access Modifiers
private void math2(){
result=a+b-c;
System.out.println("Resultby private modifier= "+result);
}
t1.math2(); //try access private modifiers in same class. successfully access
// t2.math2(); //try access private modifiers in same package sub class. not access
successfully access
//t3.math2(); //try access private modifiers in same package non-sub class. not access
successfully access
//t4.math2(); //try access private modifiers in Different package non-sub class. not
access successfully access
//t5.math2(); //try access private modifiers in Different package sub class. not access
successfully access
12