SlideShare ist ein Scribd-Unternehmen logo
1 von 28
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

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Exception Handling in VB.Net
Exception Handling in VB.NetException Handling in VB.Net
Exception Handling in VB.Net
 
Sum of subset problem.pptx
Sum of subset problem.pptxSum of subset problem.pptx
Sum of subset problem.pptx
 
1.Role lexical Analyzer
1.Role lexical Analyzer1.Role lexical Analyzer
1.Role lexical Analyzer
 
Java applet
Java appletJava applet
Java applet
 
Binary search in data structure
Binary search in data structureBinary search in data structure
Binary search in data structure
 
File Protection in Operating System
File Protection in Operating SystemFile Protection in Operating System
File Protection in Operating System
 
13. Pointer and 2D array
13. Pointer  and  2D array13. Pointer  and  2D array
13. Pointer and 2D array
 
Constants, Variables and Data Types in Java
Constants, Variables and Data Types in JavaConstants, Variables and Data Types in Java
Constants, Variables and Data Types in Java
 
Java exception
Java exception Java exception
Java exception
 
Life cycle-of-a-thread
Life cycle-of-a-threadLife cycle-of-a-thread
Life cycle-of-a-thread
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
Fundamental design concepts
Fundamental design conceptsFundamental design concepts
Fundamental design concepts
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
 
Java swing
Java swingJava swing
Java swing
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
 
Design notation
Design notationDesign notation
Design notation
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
2CPP14 - Abstraction
2CPP14 - Abstraction2CPP14 - Abstraction
2CPP14 - Abstraction
 

Andere mochten auch

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 javaTyagi2636
 
Eo gaddis java_chapter_03_5e
Eo gaddis java_chapter_03_5eEo gaddis java_chapter_03_5e
Eo gaddis java_chapter_03_5eGina Bullock
 
Runnable interface.34
Runnable interface.34Runnable interface.34
Runnable interface.34myrajendra
 
Exception handling
Exception handlingException handling
Exception handlingIblesoft
 
Exceptions in Java
Exceptions in JavaExceptions in Java
Exceptions in JavaVadym 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.netNeelesh Shukla
 
Exception Handling Java
Exception Handling JavaException Handling Java
Exception Handling Javaankitgarg_er
 
Java - Exception Handling
Java - Exception HandlingJava - Exception Handling
Java - Exception HandlingPrabhdeep Singh
 
Java exception handling
Java exception handlingJava exception handling
Java exception handlingBHUVIJAYAVELU
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javaPratik Soares
 
Exception Handling
Exception HandlingException Handling
Exception HandlingReddhi Basu
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling pptJavabynataraJ
 

Andere mochten auch (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
 
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
 
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
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Threads in JAVA
Threads in JAVAThreads in JAVA
Threads in JAVA
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling ppt
 
Inheritance
InheritanceInheritance
Inheritance
 

Ähnlich wie Access Modifiers

Inheritance, Object Oriented Programming
Inheritance, Object Oriented ProgrammingInheritance, Object Oriented Programming
Inheritance, Object Oriented ProgrammingArslan Waseem
 
Chapter 03 enscapsulation
Chapter 03 enscapsulationChapter 03 enscapsulation
Chapter 03 enscapsulationNurhanna 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 .pptxMDRakibKhan3
 
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.pptxDaveEstonilo
 
03 classes interfaces_principlesofoop
03 classes interfaces_principlesofoop03 classes interfaces_principlesofoop
03 classes interfaces_principlesofoopVladislav Hadzhiyski
 
Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in javaAshwin Thadani
 
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.pdfsanjeevtandonsre
 
Java modifiers
Java modifiersJava modifiers
Java modifiersSoba Arjun
 
Inheritance,constructor,friend function
Inheritance,constructor,friend functionInheritance,constructor,friend function
Inheritance,constructor,friend functionFAKRUL ISLAM
 
How totestinternalprotectmethodsinc#
How totestinternalprotectmethodsinc#How totestinternalprotectmethodsinc#
How totestinternalprotectmethodsinc#LearningTech
 
Access modifiers
Access modifiersAccess modifiers
Access modifiersJadavsejal
 

Ähnlich wie Access Modifiers (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
 
[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers
 
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 modifiers
Access modifiersAccess modifiers
Access modifiers
 

Mehr von 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
 

Kürzlich hochgeladen

Multi Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleMulti Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleCeline George
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationdeepaannamalai16
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17Celine George
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Association for Project Management
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseCeline George
 
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxMan or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxDhatriParmar
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1GloryAnnCastre1
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdfMr Bounab Samir
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDhatriParmar
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxkarenfajardo43
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSMae Pangan
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWQuiz Club NITW
 

Kürzlich hochgeladen (20)

Multi Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleMulti Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP Module
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentation
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 Database
 
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxMan or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
 
prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Professionprashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHS
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITW
 

Access Modifiers

  • 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