Trauma-Informed Leadership - Five Practical Principles
Lect6
1. Access Modifiers In JAVA
•What are Access Modifiers?
•What is Default access modifier?
•What is Public Access Modifier?
•What is Protected Access Modifier?
•What is Private Access Modifier?
•Give an example to understand syntax of all access modifiers?.
4/10/2019 1Jamsher Bhanbhro(F16CS11)
2. What are access modifiers?
• Modifiers are the keywords when used, change
the meaning of class/variable/method.
• Access Modifiers are those which when used
change the access of a class/variable/method.
• Modifiers are used for the security purpose.
• There are many modifiers in java basically
there are four access modifiers which are used
in today’s programs structure.
4/10/2019 Jamsher Bhanbhro(F16CS11) 2
3. 1. Default Modifier
• We don’t declare any modifier by our self for a
class, field or a method.
• A variable or method declared without access
modifiers is available to any other class or
function.
• Fields are by default public static and final.
• E.g:
4/10/2019 Jamsher Bhanbhro(F16CS11) 3
4. 2. Public Access Modifier
• When used with class, method, constructor,
interface, field these all will be visible throughout
the program.
• Can be accessed throughout the program.
• In the different packages public class will not
access it will be imported there.
• E.g: Main method is public so it is accessed any
where in the program.
• public static void main(String args[]){}
4/10/2019 Jamsher Bhanbhro(F16CS11) 4
5. 3. Protect Access Modifier
• It can’t be applied to classes or interface.
• Variables, methods or the fields declared with
protect key word in the super class will only be
accessed by subclasses.
• In an interface we can’t declare any field or
method as protected.
• Protected access gives subclasses advantage to
load or access the super class.
E.g:
4/10/2019 Jamsher Bhanbhro(F16CS11) 5
6. 4. Private Access Modifier
• Can’t be used with interface and class.
• When used with methods, fields or
constructors these only be visible or accessed
in that class.
• This is most restrictive access modifier.
• This is used for the security and flexibility of
the code
4/10/2019 Jamsher Bhanbhro(F16CS11) 6
7. Example Program
// class is used for creating method and checked how to use the protected access modifiers
class Jamsher{
private int sub(int a, int b){
int c=a-b;
System.out.println(c);
return c;
}
protected void sum(int a, int b){}
}
class Jam1 extends Jamsher{
protected void sum(int a, int b){
int c=a+b;
System.out.println("The sum is=:"+c);
}// here you can note that sub method is not accessible in main method.
}
public class helloworld {
// this method is public and will be accessed throughout the program.
public static void main(String args[]){
Jam1 ob=new Jam1();
ob.sum(11, 41);
// ob.sub(41,11); // we cannot call the private method here}}
4/10/2019 Jamsher Bhanbhro(F16CS11) 7
8. Example
// class is used for creating method and checked how to use the protected access modifiers
class Jamsher{
protected void sum(int a, int b){}
}
class Jam1 extends Jamsher{
protected void sum(int a, int b){
int c=a+b;
System.out.println("The sum is=:"+c);
}
}
public class helloworld {
private int sub(int a, int b){
int c=a-b;
System.out.println("the sub is"+c);
return c;
}
// this method is public and will be accessed throughout the program.
public static void main(String args[]){
Jam1 ob=new Jam1();
ob.sum(11, 41);
helloworld obj=new helloworld();
obj.sub(41, 11);
// we can call the private method here because the method is in same class}}
}
4/10/2019 Jamsher Bhanbhro(F16CS11) 8
9. Summary
• Above example shows the machanism or the
behavior of the three access modifiers.
• Compare above both programs and understand
the access modifiers.
• As default is the programs original behavior so
I didn’t described it here.
4/10/2019 Jamsher Bhanbhro(F16CS11) 9