SlideShare a Scribd company logo
1 of 9
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)
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
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
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
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
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
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
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
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

More Related Content

What's hot

Java Chapter 04 - Writing Classes: part 3
Java Chapter 04 - Writing Classes: part 3 Java Chapter 04 - Writing Classes: part 3
Java Chapter 04 - Writing Classes: part 3
DanWooster1
 

What's hot (19)

Interface in java
Interface in javaInterface in java
Interface in java
 
Choice router
Choice routerChoice router
Choice router
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Interfaces
InterfacesInterfaces
Interfaces
 
What are Abstract Classes in Java | Edureka
What are Abstract Classes in Java | EdurekaWhat are Abstract Classes in Java | Edureka
What are Abstract Classes in Java | Edureka
 
Java Chapter 04 - Writing Classes: part 3
Java Chapter 04 - Writing Classes: part 3 Java Chapter 04 - Writing Classes: part 3
Java Chapter 04 - Writing Classes: part 3
 
Java8 training - class 2
Java8 training - class 2Java8 training - class 2
Java8 training - class 2
 
Access modifiers
Access modifiersAccess modifiers
Access modifiers
 
Learn Java language fundamentals with Unit nexus
Learn Java language fundamentals with Unit nexusLearn Java language fundamentals with Unit nexus
Learn Java language fundamentals with Unit nexus
 
Lecture 18
Lecture 18Lecture 18
Lecture 18
 
Accessing non static members from the main
Accessing non static members from the mainAccessing non static members from the main
Accessing non static members from the main
 
Java interfaces
Java   interfacesJava   interfaces
Java interfaces
 
C# Access modifiers
C# Access modifiersC# Access modifiers
C# Access modifiers
 
Master of Computer Application (MCA) – Semester 4 MC0078
Master of Computer Application (MCA) – Semester 4  MC0078Master of Computer Application (MCA) – Semester 4  MC0078
Master of Computer Application (MCA) – Semester 4 MC0078
 
Chapter 3i
Chapter 3iChapter 3i
Chapter 3i
 
Bt8903,c# programming
Bt8903,c# programmingBt8903,c# programming
Bt8903,c# programming
 
C# Basics
C# BasicsC# Basics
C# Basics
 
Abstract class in java
Abstract class in javaAbstract class in java
Abstract class in java
 
Javainterface
Javainterface Javainterface
Javainterface
 

Similar to Lect6

Understanding And Using Reflection
Understanding And Using ReflectionUnderstanding And Using Reflection
Understanding And Using Reflection
Ganesh Samarthyam
 
The Java Learning Kit Chapter 5 – Methods and Modular.docx
The Java Learning Kit Chapter 5 – Methods and Modular.docxThe Java Learning Kit Chapter 5 – Methods and Modular.docx
The Java Learning Kit Chapter 5 – Methods and Modular.docx
arnoldmeredith47041
 
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptxINDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
Indu65
 

Similar to Lect6 (20)

Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
OOFeatures_revised-2.pptx
OOFeatures_revised-2.pptxOOFeatures_revised-2.pptx
OOFeatures_revised-2.pptx
 
03_A-OOPs_Interfaces.ppt
03_A-OOPs_Interfaces.ppt03_A-OOPs_Interfaces.ppt
03_A-OOPs_Interfaces.ppt
 
Interface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar SinghInterface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar Singh
 
Php OOP interview questions
Php OOP interview questionsPhp OOP interview questions
Php OOP interview questions
 
Access Modifiers .pptx
Access Modifiers .pptxAccess Modifiers .pptx
Access Modifiers .pptx
 
Chapter-3 Scoping.pptx
Chapter-3 Scoping.pptxChapter-3 Scoping.pptx
Chapter-3 Scoping.pptx
 
Interface &packages
Interface &packagesInterface &packages
Interface &packages
 
11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
 
Understanding And Using Reflection
Understanding And Using ReflectionUnderstanding And Using Reflection
Understanding And Using Reflection
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Dev labs alliance top 20 basic java interview questions for sdet
Dev labs alliance top 20 basic java interview questions for sdetDev labs alliance top 20 basic java interview questions for sdet
Dev labs alliance top 20 basic java interview questions for sdet
 
The Java Learning Kit Chapter 5 – Methods and Modular.docx
The Java Learning Kit Chapter 5 – Methods and Modular.docxThe Java Learning Kit Chapter 5 – Methods and Modular.docx
The Java Learning Kit Chapter 5 – Methods and Modular.docx
 
Java scjp-part1
Java scjp-part1Java scjp-part1
Java scjp-part1
 
Top 20 basic java interview questions for SDET
Top 20 basic java interview questions for SDETTop 20 basic java interview questions for SDET
Top 20 basic java interview questions for SDET
 
Dev labs alliance top 20 basic java interview question for sdet
Dev labs alliance top 20 basic java interview question for sdetDev labs alliance top 20 basic java interview question for sdet
Dev labs alliance top 20 basic java interview question for sdet
 
chp 3 : Modifiers (scjp/ocjp)
chp 3 : Modifiers (scjp/ocjp)chp 3 : Modifiers (scjp/ocjp)
chp 3 : Modifiers (scjp/ocjp)
 
lecture 6
 lecture 6 lecture 6
lecture 6
 
Introduction to computer science
Introduction to computer scienceIntroduction to computer science
Introduction to computer science
 
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptxINDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
INDUMATHY- UNIT 1 cs3391 oops introduction to oop and java.pptx
 

More from Jamsher bhanbhro

More from Jamsher bhanbhro (14)

Abstraction in Java: Abstract class and Interfaces
Abstraction in  Java: Abstract class and InterfacesAbstraction in  Java: Abstract class and Interfaces
Abstraction in Java: Abstract class and Interfaces
 
Method, Constructor, Method Overloading, Method Overriding, Inheritance In Java
Method, Constructor, Method Overloading, Method Overriding, Inheritance In  JavaMethod, Constructor, Method Overloading, Method Overriding, Inheritance In  Java
Method, Constructor, Method Overloading, Method Overriding, Inheritance In Java
 
Regular Expressions in Java.
Regular Expressions in Java.Regular Expressions in Java.
Regular Expressions in Java.
 
Java Arrays and DateTime Functions
Java Arrays and DateTime FunctionsJava Arrays and DateTime Functions
Java Arrays and DateTime Functions
 
Lect10
Lect10Lect10
Lect10
 
Lect9
Lect9Lect9
Lect9
 
Lect8
Lect8Lect8
Lect8
 
Lect7
Lect7Lect7
Lect7
 
Lect5
Lect5Lect5
Lect5
 
Lect4
Lect4Lect4
Lect4
 
Compiling and understanding first program in java
Compiling and understanding first program in javaCompiling and understanding first program in java
Compiling and understanding first program in java
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 
Caap presentation by me
Caap presentation by meCaap presentation by me
Caap presentation by me
 
Introduction to parts of Computer(Computer Fundamentals)
Introduction to parts of Computer(Computer Fundamentals)Introduction to parts of Computer(Computer Fundamentals)
Introduction to parts of Computer(Computer Fundamentals)
 

Recently uploaded

會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
中 央社
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
Peter Brusilovsky
 

Recently uploaded (20)

會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
Scopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS PublicationsScopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS Publications
 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptx
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptx
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptx
 
Book Review of Run For Your Life Powerpoint
Book Review of Run For Your Life PowerpointBook Review of Run For Your Life Powerpoint
Book Review of Run For Your Life Powerpoint
 
demyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptxdemyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptx
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategies
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
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