SlideShare a Scribd company logo
Objectives



On completion of this period, you would be
able to learn
• Access protection
• Access modifiers
• Applying access protection




                                             1
Recap

Packages
• Name provide to a group of classes
• A collection of related classes and interfaces
• A name space management tool provided by
  Java




                                                   2
Access Protection
• What is access protection
  • In objects, certain members have to be
    protected from unauthorized access
  • In C++, we learnt how to do such
    protection
  • We used private, public, and protected
    keywords for this purpose
  • Java also has such features


                                             3
Access Protection
• Access Protection
  • To protect the access of members of an
    object
  • Done with the help of access modifiers
  • Keywords are – private, public and
    protected
  • Access modifiers are also known as
    Visibility Modifiers


                                             4
Access Protection
• Purpose of Access Protection
  • Enforces
     • information hiding
     • encapsulation




                                  5
Access Modifiers
• Types of Access Modifiers
 • public
 • private
 • protected
 • default (no special keyword)




                                  6
Access Modifiers        Contd..
• public access
   • Member is visible or accessible to the world
   • Any method of any object can use a public
     member
   • keyword : public
   • eg. : public int number;
   •        public void sum(){
   •                …                      Put before
                                           return type
   •        }
   • Generally access methods are public
                                                         7
Access Modifiers Contd..
• private access
   • Member is visible or accessible only within its
     own class
   • Members cannot be inherited by subclasses
   • Members cannot be accessible in subclasses
   • Keyword : private




                                                       8
Access Modifiers Contd..
• private access
   • eg. : private double average;
   •        private void debug (String msg) {
   •                 …
   •         }
   • Provides the highest degree of protection




                                                 9
Access Modifiers       Contd..
• protected access
   • Member is visible or accessible to
      • The current class
      • Subclasses of the current class
      • All classes that are in the same package as
        that of the class
   • Keyword : protected
   • eg. : protected String message Str;
   • The level of protection is between public
     access and default access
                                                  10
Access Modifiers       Contd..
• default access or package access
   • Members are accessible to
      • All the classes in the same package
      • No special keyword is used
   eg. : int length;




                                              11
Access Modifiers              Contd..




   Table 27.1: Class Member Accessibility


                                            12
Rules of thumb
• public : if the member to be visible
  everywhere
• protected : if the member is visible in the
  current package and also subclasses in
  other packages
• default : if the member to be visible in the
  current package
• private : if the member is to be used only
  within the class

                                                 13
Example
• Consider the access modifier
  for a member x in class A. If it
  is:
   • private – it can only be
      used inside A.
   • default – it can be used
      anywhere in package1,
      that is, A,B and C.
   • protected – it can be used
      anywhere in package1,
      and in subclasses of A in
      other packages, here S.
   • public – it can be used
      everywhere in the system                                       14
                                  Fig. 27.1. Packages with its classes
Example      Contd..
• Consider the access
  modifier for the class
  B. If it is:
  • default – the class
     can only be used in
     package1.
  • public – the class
     can be used from
     anywhere.

                            Fig. 27.2. Packages with its classes
                                                              15
Using Access Modifiers
• Guidelines :
  • Make all data members private
  • Create public getter and setter methods for data
    members as necessary
  • If possible, make a method private, else make it
    default, or make it public




                                                  16
Example Program
• With the help of the following program, we
  will try to analyze the access protection
  features




                                               17
Example Program
• Consider the Person class

• What access modifiers should be used, and
  which access should define?




                                              18
Example Program
public class Person {
  int SSNumber;
  String name;
  String address;
}




                              19
Example Program             Contd..
public class Person {
  private final int SSNumber;
  private String name;
  private String address;
  public Person(int ssn, String name) {
       SSNumber = ssn;
       this. name = name;
  }
  public String getName() {
       return name;
}
                                             20
Example Program            Contd..
public String getAddress () {
       return address;
  }
public void setAddress (String address) {
  this. address = address;
  }
}




                                            21
• The analysis is as follows
           Example Program           Contd..
  • The SSNumber* must be given when the person object
    is created, and cannot be changed later
  • The Name must be given when the object is created
    Normally it will not change later
  • The address need not be present, but it can be changed
    along the way




                                                        22
Summary
• In this class we have discussed
   • Access protection
   • Access modifiers
      • public, private, protected
   • How to use access modifiers




                                     23
Frequently Asked Questions
1. List the access modifiers in Java ?

2. What is the difference between default and
   protected modifiers ?

3. Explain how to use access modifiers




                                                24
Quiz

1. We would like to make a member of a class
  visible In all subclasses regardless of what
  package they are in. Which keyword is useful
  for that ?
   1. private
   2. public
   3. protected
   4. No keyword (default)




                    CM604.27                 25
Quiz             Contd..



2. Which keyword can make a class visible
  within a package?

  •   private
  •   public
  •   protected
  •   No keyword (default)




                    CM604.27                   26
Quiz              Contd..




3. Which keyword is useful for implementing
  information hiding principle?
   1. private
   2. public
   3. protected
   4. No keyword (default)




                    CM604.27                    27
Quiz              Contd..



4. Which one of the following is NOT a keyword
  related to Access protection?

  1. private
  2. public
  3. protected
  4. default




                    CM604.27                     28

More Related Content

PPTX
Access specifiers(modifiers) in java
HrithikShinde
 
PPS
Java Exception handling
kamal kotecha
 
PPT
Exception Handling in JAVA
SURIT DATTA
 
PPS
Interface
kamal kotecha
 
PPTX
[OOP - Lec 07] Access Specifiers
Muhammad Hammad Waseem
 
PPT
Final keyword in java
Lovely Professional University
 
PPTX
20.3 Java encapsulation
Intro C# Book
 
PDF
Java exception handling ppt
JavabynataraJ
 
Access specifiers(modifiers) in java
HrithikShinde
 
Java Exception handling
kamal kotecha
 
Exception Handling in JAVA
SURIT DATTA
 
Interface
kamal kotecha
 
[OOP - Lec 07] Access Specifiers
Muhammad Hammad Waseem
 
Final keyword in java
Lovely Professional University
 
20.3 Java encapsulation
Intro C# Book
 
Java exception handling ppt
JavabynataraJ
 

What's hot (20)

PPTX
Java awt (abstract window toolkit)
Elizabeth alexander
 
PPTX
JAVA AWT
shanmuga rajan
 
PPTX
Introduction to package in java
Prognoz Technologies Pvt. Ltd.
 
PPTX
File handling
priya_trehan
 
PPTX
java interface and packages
VINOTH R
 
PPTX
Inheritance in c++
Paumil Patel
 
PPTX
INHERITANCE IN JAVA.pptx
NITHISG1
 
PPTX
Java package
CS_GDRCST
 
PPTX
Multithreading in java
Monika Mishra
 
PPTX
Packages in java
Kavitha713564
 
PPTX
Multiple inheritance in java3 (1).pptx
RkGupta83
 
PPS
Wrapper class
kamal kotecha
 
PDF
Access modifiers in java
Muthukumaran Subramanian
 
PPTX
Inheritance ppt
Nivegeetha
 
PPT
Abstract class in java
Lovely Professional University
 
PPTX
Inheritance In Java
Manish Sahu
 
PPTX
I/O Streams
Ravi Chythanya
 
PPTX
Dynamic method dispatch
yugandhar vadlamudi
 
PPTX
Packages in java
Elizabeth alexander
 
PPTX
Ppt on this and super keyword
tanu_jaswal
 
Java awt (abstract window toolkit)
Elizabeth alexander
 
JAVA AWT
shanmuga rajan
 
Introduction to package in java
Prognoz Technologies Pvt. Ltd.
 
File handling
priya_trehan
 
java interface and packages
VINOTH R
 
Inheritance in c++
Paumil Patel
 
INHERITANCE IN JAVA.pptx
NITHISG1
 
Java package
CS_GDRCST
 
Multithreading in java
Monika Mishra
 
Packages in java
Kavitha713564
 
Multiple inheritance in java3 (1).pptx
RkGupta83
 
Wrapper class
kamal kotecha
 
Access modifiers in java
Muthukumaran Subramanian
 
Inheritance ppt
Nivegeetha
 
Abstract class in java
Lovely Professional University
 
Inheritance In Java
Manish Sahu
 
I/O Streams
Ravi Chythanya
 
Dynamic method dispatch
yugandhar vadlamudi
 
Packages in java
Elizabeth alexander
 
Ppt on this and super keyword
tanu_jaswal
 
Ad

Viewers also liked (20)

DOC
How would you implement multiple inheritance in java
Tyagi2636
 
PPT
Packages in java
Abhishek Khune
 
PPT
Java packages
Raja Sekhar
 
PPT
Eo gaddis java_chapter_03_5e
Gina Bullock
 
PPT
Runnable interface.34
myrajendra
 
PPT
Exception handling
Iblesoft
 
PPT
Java exception
Arati Gadgil
 
PPTX
Exceptions in Java
Vadym Lotar
 
PPTX
Effective Java - Chapter 4: Classes and Interfaces
İbrahim Kürce
 
PPTX
Exception handling in asp.net
Neelesh Shukla
 
PPT
Exception Handling Java
ankitgarg_er
 
PPTX
Java - Exception Handling
Prabhdeep Singh
 
PDF
Java keywords
Ravi_Kant_Sahu
 
PPTX
Java exception handling
BHUVIJAYAVELU
 
PPT
Java: Exception
Tareq Hasan
 
PPT
Exception handling in java
Pratik Soares
 
PPTX
Threads in JAVA
Haldia Institute of Technology
 
PPSX
Exception Handling
Reddhi Basu
 
PPSX
Inheritance
Selvin Josy Bai Somu
 
PPT
Packages and interfaces
vanithaRamasamy
 
How would you implement multiple inheritance in java
Tyagi2636
 
Packages in java
Abhishek Khune
 
Java packages
Raja Sekhar
 
Eo gaddis java_chapter_03_5e
Gina Bullock
 
Runnable interface.34
myrajendra
 
Exception handling
Iblesoft
 
Java exception
Arati Gadgil
 
Exceptions in Java
Vadym Lotar
 
Effective Java - Chapter 4: Classes and Interfaces
İbrahim Kürce
 
Exception handling in asp.net
Neelesh Shukla
 
Exception Handling Java
ankitgarg_er
 
Java - Exception Handling
Prabhdeep Singh
 
Java keywords
Ravi_Kant_Sahu
 
Java exception handling
BHUVIJAYAVELU
 
Java: Exception
Tareq Hasan
 
Exception handling in java
Pratik Soares
 
Exception Handling
Reddhi Basu
 
Packages and interfaces
vanithaRamasamy
 
Ad

Similar to Access Protection (20)

PPTX
Visibility Modifiers for Access Control.pptx
naazminshaikh1727
 
PPTX
Visibility control in java
Tech_MX
 
PPTX
Access modifiers in java
Sourabrata Mukherjee
 
PDF
Chapter 03 enscapsulation
Nurhanna Aziz
 
PPTX
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_03-Mar-2021_L13...
MaruMengesha
 
PPTX
Access Modifiers of oop classes concept.pptx
umarshafi527
 
PPTX
Access modifiers in java
Madishetty Prathibha
 
PPTX
Access Modifiers .pptx
MDRakibKhan3
 
PPTX
C# Access modifiers
Prem Kumar Badri
 
PDF
CHAPTER 3 part1.pdf
FacultyAnupamaAlagan
 
PDF
Access specifiers (Public Private Protected) C++
vivekkumar2938
 
PPTX
Power point presentation on access specifier in OOPs
AdrizaBera
 
PPTX
Access Modifier.pptx
Margaret Mary
 
ODP
Class&objects
harivng
 
PPTX
Imp Key.pptx very easy to learn go for it
dwivedyp
 
PPT
Inheritance
ankush_kumar
 
PPTX
Access modifier and inheritance
Dikshyanta Dhungana
 
PPT
9 cm604.14
myrajendra
 
PPTX
Access controlaspecifier and visibilty modes
Vinay Kumar
 
Visibility Modifiers for Access Control.pptx
naazminshaikh1727
 
Visibility control in java
Tech_MX
 
Access modifiers in java
Sourabrata Mukherjee
 
Chapter 03 enscapsulation
Nurhanna Aziz
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_03-Mar-2021_L13...
MaruMengesha
 
Access Modifiers of oop classes concept.pptx
umarshafi527
 
Access modifiers in java
Madishetty Prathibha
 
Access Modifiers .pptx
MDRakibKhan3
 
C# Access modifiers
Prem Kumar Badri
 
CHAPTER 3 part1.pdf
FacultyAnupamaAlagan
 
Access specifiers (Public Private Protected) C++
vivekkumar2938
 
Power point presentation on access specifier in OOPs
AdrizaBera
 
Access Modifier.pptx
Margaret Mary
 
Class&objects
harivng
 
Imp Key.pptx very easy to learn go for it
dwivedyp
 
Inheritance
ankush_kumar
 
Access modifier and inheritance
Dikshyanta Dhungana
 
9 cm604.14
myrajendra
 
Access controlaspecifier and visibilty modes
Vinay Kumar
 

More from myrajendra (20)

PPT
Fundamentals
myrajendra
 
PPT
Data type
myrajendra
 
PPTX
Hibernate example1
myrajendra
 
PPTX
Jdbc workflow
myrajendra
 
PPTX
2 jdbc drivers
myrajendra
 
PPTX
3 jdbc api
myrajendra
 
PPTX
4 jdbc step1
myrajendra
 
PPTX
Dao example
myrajendra
 
PPTX
Sessionex1
myrajendra
 
PPTX
Internal
myrajendra
 
PPTX
3. elements
myrajendra
 
PPTX
2. attributes
myrajendra
 
PPTX
1 introduction to html
myrajendra
 
PPTX
Headings
myrajendra
 
PPTX
Forms
myrajendra
 
PPT
Css
myrajendra
 
PPTX
Views
myrajendra
 
PPTX
Views
myrajendra
 
PPTX
Views
myrajendra
 
PPT
Starting jdbc
myrajendra
 
Fundamentals
myrajendra
 
Data type
myrajendra
 
Hibernate example1
myrajendra
 
Jdbc workflow
myrajendra
 
2 jdbc drivers
myrajendra
 
3 jdbc api
myrajendra
 
4 jdbc step1
myrajendra
 
Dao example
myrajendra
 
Sessionex1
myrajendra
 
Internal
myrajendra
 
3. elements
myrajendra
 
2. attributes
myrajendra
 
1 introduction to html
myrajendra
 
Headings
myrajendra
 
Forms
myrajendra
 
Views
myrajendra
 
Views
myrajendra
 
Views
myrajendra
 
Starting jdbc
myrajendra
 

Recently uploaded (20)

PPTX
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
DOCX
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
PPTX
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
PPTX
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
PPTX
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
PPTX
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
PDF
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
PPTX
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
PPTX
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PPTX
Virus sequence retrieval from NCBI database
yamunaK13
 
PDF
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PDF
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
PDF
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
PPTX
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
Virus sequence retrieval from NCBI database
yamunaK13
 
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 

Access Protection

  • 1. Objectives On completion of this period, you would be able to learn • Access protection • Access modifiers • Applying access protection 1
  • 2. Recap Packages • Name provide to a group of classes • A collection of related classes and interfaces • A name space management tool provided by Java 2
  • 3. Access Protection • What is access protection • In objects, certain members have to be protected from unauthorized access • In C++, we learnt how to do such protection • We used private, public, and protected keywords for this purpose • Java also has such features 3
  • 4. Access Protection • Access Protection • To protect the access of members of an object • Done with the help of access modifiers • Keywords are – private, public and protected • Access modifiers are also known as Visibility Modifiers 4
  • 5. Access Protection • Purpose of Access Protection • Enforces • information hiding • encapsulation 5
  • 6. Access Modifiers • Types of Access Modifiers • public • private • protected • default (no special keyword) 6
  • 7. Access Modifiers Contd.. • public access • Member is visible or accessible to the world • Any method of any object can use a public member • keyword : public • eg. : public int number; • public void sum(){ • … Put before return type • } • Generally access methods are public 7
  • 8. Access Modifiers Contd.. • private access • Member is visible or accessible only within its own class • Members cannot be inherited by subclasses • Members cannot be accessible in subclasses • Keyword : private 8
  • 9. Access Modifiers Contd.. • private access • eg. : private double average; • private void debug (String msg) { • … • } • Provides the highest degree of protection 9
  • 10. Access Modifiers Contd.. • protected access • Member is visible or accessible to • The current class • Subclasses of the current class • All classes that are in the same package as that of the class • Keyword : protected • eg. : protected String message Str; • The level of protection is between public access and default access 10
  • 11. Access Modifiers Contd.. • default access or package access • Members are accessible to • All the classes in the same package • No special keyword is used eg. : int length; 11
  • 12. Access Modifiers Contd.. Table 27.1: Class Member Accessibility 12
  • 13. Rules of thumb • public : if the member to be visible everywhere • protected : if the member is visible in the current package and also subclasses in other packages • default : if the member to be visible in the current package • private : if the member is to be used only within the class 13
  • 14. Example • Consider the access modifier for a member x in class A. If it is: • private – it can only be used inside A. • default – it can be used anywhere in package1, that is, A,B and C. • protected – it can be used anywhere in package1, and in subclasses of A in other packages, here S. • public – it can be used everywhere in the system 14 Fig. 27.1. Packages with its classes
  • 15. Example Contd.. • Consider the access modifier for the class B. If it is: • default – the class can only be used in package1. • public – the class can be used from anywhere. Fig. 27.2. Packages with its classes 15
  • 16. Using Access Modifiers • Guidelines : • Make all data members private • Create public getter and setter methods for data members as necessary • If possible, make a method private, else make it default, or make it public 16
  • 17. Example Program • With the help of the following program, we will try to analyze the access protection features 17
  • 18. Example Program • Consider the Person class • What access modifiers should be used, and which access should define? 18
  • 19. Example Program public class Person { int SSNumber; String name; String address; } 19
  • 20. Example Program Contd.. public class Person { private final int SSNumber; private String name; private String address; public Person(int ssn, String name) { SSNumber = ssn; this. name = name; } public String getName() { return name; } 20
  • 21. Example Program Contd.. public String getAddress () { return address; } public void setAddress (String address) { this. address = address; } } 21
  • 22. • The analysis is as follows Example Program Contd.. • The SSNumber* must be given when the person object is created, and cannot be changed later • The Name must be given when the object is created Normally it will not change later • The address need not be present, but it can be changed along the way 22
  • 23. Summary • In this class we have discussed • Access protection • Access modifiers • public, private, protected • How to use access modifiers 23
  • 24. Frequently Asked Questions 1. List the access modifiers in Java ? 2. What is the difference between default and protected modifiers ? 3. Explain how to use access modifiers 24
  • 25. Quiz 1. We would like to make a member of a class visible In all subclasses regardless of what package they are in. Which keyword is useful for that ? 1. private 2. public 3. protected 4. No keyword (default) CM604.27 25
  • 26. Quiz Contd.. 2. Which keyword can make a class visible within a package? • private • public • protected • No keyword (default) CM604.27 26
  • 27. Quiz Contd.. 3. Which keyword is useful for implementing information hiding principle? 1. private 2. public 3. protected 4. No keyword (default) CM604.27 27
  • 28. Quiz Contd.. 4. Which one of the following is NOT a keyword related to Access protection? 1. private 2. public 3. protected 4. default CM604.27 28