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

What's hot

Applications of stack
Applications of stackApplications of stack
Applications of stack
eShikshak
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
Pavith Gunasekara
 
Built in exceptions
Built in exceptions Built in exceptions
Built in exceptions
TharuniDiddekunta
 
Lecture_7-Encapsulation in Java.pptx
Lecture_7-Encapsulation in Java.pptxLecture_7-Encapsulation in Java.pptx
Lecture_7-Encapsulation in Java.pptx
ShahinAhmed49
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
03062679929
 
[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers
Muhammad Hammad Waseem
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
Jussi Pohjolainen
 
Directory implementation and allocation methods
Directory implementation and allocation methodsDirectory implementation and allocation methods
Directory implementation and allocation methods
sangrampatil81
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
Rokonuzzaman Rony
 
Chapter2 Encapsulation (Java)
Chapter2 Encapsulation (Java)Chapter2 Encapsulation (Java)
Chapter2 Encapsulation (Java)
Dyah Fajar Nur Rohmah
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
Nahian Ahmed
 
Packages and interfaces
Packages and interfacesPackages and interfaces
Packages and interfaces
vanithaRamasamy
 
Method overriding
Method overridingMethod overriding
Method overriding
Azaz Maverick
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
Pratik Soares
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
Vinod Kumar
 
Exception Handling
Exception HandlingException Handling
Exception Handling
Reddhi Basu
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
VINOTH R
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
MOHIT AGARWAL
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
ThamizhselviKrishnam
 

What's hot (20)

Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Built in exceptions
Built in exceptions Built in exceptions
Built in exceptions
 
Lecture_7-Encapsulation in Java.pptx
Lecture_7-Encapsulation in Java.pptxLecture_7-Encapsulation in Java.pptx
Lecture_7-Encapsulation in Java.pptx
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
 
Directory implementation and allocation methods
Directory implementation and allocation methodsDirectory implementation and allocation methods
Directory implementation and allocation methods
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Chapter2 Encapsulation (Java)
Chapter2 Encapsulation (Java)Chapter2 Encapsulation (Java)
Chapter2 Encapsulation (Java)
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
 
Packages and interfaces
Packages and interfacesPackages and interfaces
Packages and interfaces
 
Method overriding
Method overridingMethod overriding
Method overriding
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
 

Viewers also liked

How would you implement multiple inheritance in java
How would you implement multiple inheritance in javaHow would you implement multiple inheritance in java
How would you implement multiple inheritance in java
Tyagi2636
 
Packages in java
Packages in javaPackages in java
Packages in java
Abhishek Khune
 
Java packages
Java packagesJava packages
Java packages
Raja Sekhar
 
Eo gaddis java_chapter_03_5e
Eo gaddis java_chapter_03_5eEo gaddis java_chapter_03_5e
Eo gaddis java_chapter_03_5e
Gina Bullock
 
Runnable interface.34
Runnable interface.34Runnable interface.34
Runnable interface.34
myrajendra
 
Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in java
Muthukumaran Subramanian
 
Exception handling
Exception handlingException handling
Exception handling
Iblesoft
 
Java exception
Java exception Java exception
Java exception
Arati Gadgil
 
Exceptions in Java
Exceptions in JavaExceptions in Java
Exceptions in Java
Vadym Lotar
 
Effective Java - Chapter 4: Classes and Interfaces
Effective Java - Chapter 4: Classes and InterfacesEffective Java - Chapter 4: Classes and Interfaces
Effective Java - Chapter 4: Classes and Interfaces
İbrahim Kürce
 
Exception handling in asp.net
Exception handling in asp.netException handling in asp.net
Exception handling in asp.net
Neelesh Shukla
 
Exception Handling Java
Exception Handling JavaException Handling Java
Exception Handling Java
ankitgarg_er
 
Java - Exception Handling
Java - Exception HandlingJava - Exception Handling
Java - Exception Handling
Prabhdeep Singh
 
Java keywords
Java keywordsJava keywords
Java keywords
Ravi_Kant_Sahu
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
BHUVIJAYAVELU
 
Dynamic method dispatch
Dynamic method dispatchDynamic method dispatch
Dynamic method dispatch
yugandhar vadlamudi
 
Java: Exception
Java: ExceptionJava: Exception
Java: Exception
Tareq Hasan
 
Threads in JAVA
Threads in JAVAThreads in JAVA
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling ppt
JavabynataraJ
 
Inheritance
InheritanceInheritance

Viewers also liked (20)

How would you implement multiple inheritance in java
How would you implement multiple inheritance in javaHow would you implement multiple inheritance in java
How would you implement multiple inheritance in java
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Java packages
Java packagesJava packages
Java packages
 
Eo gaddis java_chapter_03_5e
Eo gaddis java_chapter_03_5eEo gaddis java_chapter_03_5e
Eo gaddis java_chapter_03_5e
 
Runnable interface.34
Runnable interface.34Runnable interface.34
Runnable interface.34
 
Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in java
 
Exception handling
Exception handlingException handling
Exception handling
 
Java exception
Java exception Java exception
Java exception
 
Exceptions in Java
Exceptions in JavaExceptions in Java
Exceptions in Java
 
Effective Java - Chapter 4: Classes and Interfaces
Effective Java - Chapter 4: Classes and InterfacesEffective Java - Chapter 4: Classes and Interfaces
Effective Java - Chapter 4: Classes and Interfaces
 
Exception handling in asp.net
Exception handling in asp.netException handling in asp.net
Exception handling in asp.net
 
Exception Handling Java
Exception Handling JavaException Handling Java
Exception Handling Java
 
Java - Exception Handling
Java - Exception HandlingJava - Exception Handling
Java - Exception Handling
 
Java keywords
Java keywordsJava keywords
Java keywords
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Dynamic method dispatch
Dynamic method dispatchDynamic method dispatch
Dynamic method dispatch
 
Java: Exception
Java: ExceptionJava: Exception
Java: Exception
 
Threads in JAVA
Threads in JAVAThreads in JAVA
Threads in JAVA
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling ppt
 
Inheritance
InheritanceInheritance
Inheritance
 

Similar to Access Protection

27c
27c27c
27csharp
27csharp27csharp
27csharp
Sireesh K
 
C# Access modifiers
C# Access modifiersC# Access modifiers
C# Access modifiers
Prem Kumar Badri
 
Inheritance, Object Oriented Programming
Inheritance, Object Oriented ProgrammingInheritance, Object Oriented Programming
Inheritance, Object Oriented Programming
Arslan Waseem
 
Oops (inheritance&interface)
Oops (inheritance&interface)Oops (inheritance&interface)
Oops (inheritance&interface)
Muthukumaran Subramanian
 
Chapter 03 enscapsulation
Chapter 03 enscapsulationChapter 03 enscapsulation
Chapter 03 enscapsulation
Nurhanna Aziz
 
Std 12 computer chapter 8 classes and object in java (part 2)
Std 12 computer chapter 8 classes and object in java (part 2)Std 12 computer chapter 8 classes and object in java (part 2)
Std 12 computer chapter 8 classes and object in java (part 2)
Nuzhat Memon
 
Access Modifiers .pptx
Access Modifiers .pptxAccess Modifiers .pptx
Access Modifiers .pptx
MDRakibKhan3
 
Object oriented programming CLASSES-AND-OBJECTS.pptx
Object oriented programming CLASSES-AND-OBJECTS.pptxObject oriented programming CLASSES-AND-OBJECTS.pptx
Object oriented programming CLASSES-AND-OBJECTS.pptx
DaveEstonilo
 
03 classes interfaces_principlesofoop
03 classes interfaces_principlesofoop03 classes interfaces_principlesofoop
03 classes interfaces_principlesofoop
Vladislav Hadzhiyski
 
Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in java
Ashwin Thadani
 
Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in java
Sourabrata Mukherjee
 
Access Modifiers To set the access levels of variables,methods (mem.pdf
Access Modifiers To set the access levels of variables,methods (mem.pdfAccess Modifiers To set the access levels of variables,methods (mem.pdf
Access Modifiers To set the access levels of variables,methods (mem.pdf
sanjeevtandonsre
 
Java modifiers
Java modifiersJava modifiers
Java modifiers
Soba Arjun
 
Encapsulation
EncapsulationEncapsulation
Inheritance,constructor,friend function
Inheritance,constructor,friend functionInheritance,constructor,friend function
Inheritance,constructor,friend function
FAKRUL ISLAM
 
How totestinternalprotectmethodsinc#
How totestinternalprotectmethodsinc#How totestinternalprotectmethodsinc#
How totestinternalprotectmethodsinc#
LearningTech
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and Polymorphism
BG Java EE Course
 
Access controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesAccess controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modes
Vinay Kumar
 
Access specifier
Access specifierAccess specifier
Access specifier
zindadili
 

Similar to Access Protection (20)

27c
27c27c
27c
 
27csharp
27csharp27csharp
27csharp
 
C# Access modifiers
C# Access modifiersC# Access modifiers
C# Access modifiers
 
Inheritance, Object Oriented Programming
Inheritance, Object Oriented ProgrammingInheritance, Object Oriented Programming
Inheritance, Object Oriented Programming
 
Oops (inheritance&interface)
Oops (inheritance&interface)Oops (inheritance&interface)
Oops (inheritance&interface)
 
Chapter 03 enscapsulation
Chapter 03 enscapsulationChapter 03 enscapsulation
Chapter 03 enscapsulation
 
Std 12 computer chapter 8 classes and object in java (part 2)
Std 12 computer chapter 8 classes and object in java (part 2)Std 12 computer chapter 8 classes and object in java (part 2)
Std 12 computer chapter 8 classes and object in java (part 2)
 
Access Modifiers .pptx
Access Modifiers .pptxAccess Modifiers .pptx
Access Modifiers .pptx
 
Object oriented programming CLASSES-AND-OBJECTS.pptx
Object oriented programming CLASSES-AND-OBJECTS.pptxObject oriented programming CLASSES-AND-OBJECTS.pptx
Object oriented programming CLASSES-AND-OBJECTS.pptx
 
03 classes interfaces_principlesofoop
03 classes interfaces_principlesofoop03 classes interfaces_principlesofoop
03 classes interfaces_principlesofoop
 
Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in java
 
Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in java
 
Access Modifiers To set the access levels of variables,methods (mem.pdf
Access Modifiers To set the access levels of variables,methods (mem.pdfAccess Modifiers To set the access levels of variables,methods (mem.pdf
Access Modifiers To set the access levels of variables,methods (mem.pdf
 
Java modifiers
Java modifiersJava modifiers
Java modifiers
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
 
Inheritance,constructor,friend function
Inheritance,constructor,friend functionInheritance,constructor,friend function
Inheritance,constructor,friend function
 
How totestinternalprotectmethodsinc#
How totestinternalprotectmethodsinc#How totestinternalprotectmethodsinc#
How totestinternalprotectmethodsinc#
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and Polymorphism
 
Access controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesAccess controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modes
 
Access specifier
Access specifierAccess specifier
Access specifier
 

More from myrajendra

Fundamentals
FundamentalsFundamentals
Fundamentals
myrajendra
 
Data type
Data typeData type
Data type
myrajendra
 
Hibernate example1
Hibernate example1Hibernate example1
Hibernate example1
myrajendra
 
Jdbc workflow
Jdbc workflowJdbc workflow
Jdbc workflow
myrajendra
 
2 jdbc drivers
2 jdbc drivers2 jdbc drivers
2 jdbc drivers
myrajendra
 
3 jdbc api
3 jdbc api3 jdbc api
3 jdbc api
myrajendra
 
4 jdbc step1
4 jdbc step14 jdbc step1
4 jdbc step1
myrajendra
 
Dao example
Dao exampleDao example
Dao example
myrajendra
 
Sessionex1
Sessionex1Sessionex1
Sessionex1
myrajendra
 
Internal
InternalInternal
Internal
myrajendra
 
3. elements
3. elements3. elements
3. elements
myrajendra
 
2. attributes
2. attributes2. attributes
2. attributes
myrajendra
 
1 introduction to html
1 introduction to html1 introduction to html
1 introduction to html
myrajendra
 
Headings
HeadingsHeadings
Headings
myrajendra
 
Forms
FormsForms
Forms
myrajendra
 
Css
CssCss
Views
ViewsViews
Views
myrajendra
 
Views
ViewsViews
Views
myrajendra
 
Views
ViewsViews
Views
myrajendra
 
Starting jdbc
Starting jdbcStarting jdbc
Starting jdbc
myrajendra
 

More from myrajendra (20)

Fundamentals
FundamentalsFundamentals
Fundamentals
 
Data type
Data typeData type
Data type
 
Hibernate example1
Hibernate example1Hibernate example1
Hibernate example1
 
Jdbc workflow
Jdbc workflowJdbc workflow
Jdbc workflow
 
2 jdbc drivers
2 jdbc drivers2 jdbc drivers
2 jdbc drivers
 
3 jdbc api
3 jdbc api3 jdbc api
3 jdbc api
 
4 jdbc step1
4 jdbc step14 jdbc step1
4 jdbc step1
 
Dao example
Dao exampleDao example
Dao example
 
Sessionex1
Sessionex1Sessionex1
Sessionex1
 
Internal
InternalInternal
Internal
 
3. elements
3. elements3. elements
3. elements
 
2. attributes
2. attributes2. attributes
2. attributes
 
1 introduction to html
1 introduction to html1 introduction to html
1 introduction to html
 
Headings
HeadingsHeadings
Headings
 
Forms
FormsForms
Forms
 
Css
CssCss
Css
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Starting jdbc
Starting jdbcStarting jdbc
Starting jdbc
 

Recently uploaded

How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
Bisnar Chase Personal Injury Attorneys
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
Kavitha Krishnan
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 

Recently uploaded (20)

How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 

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