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?

Java package
Java packageJava package
Java packageCS_GDRCST
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packagesVINOTH R
 
Constructors in C++
Constructors in C++Constructors in C++
Constructors in C++RubaNagarajan
 
Abstraction in java
Abstraction in javaAbstraction in java
Abstraction in javasawarkar17
 
Life cycle-of-a-thread
Life cycle-of-a-threadLife cycle-of-a-thread
Life cycle-of-a-threadjavaicon
 
Finalize() method
Finalize() methodFinalize() method
Finalize() methodJadavsejal
 
Applets in java
Applets in javaApplets in java
Applets in javaWani Zahoor
 
Jdbc architecture and driver types ppt
Jdbc architecture and driver types pptJdbc architecture and driver types ppt
Jdbc architecture and driver types pptkamal kotecha
 
Method overriding
Method overridingMethod overriding
Method overridingAzaz Maverick
 
Constructor ppt
Constructor pptConstructor ppt
Constructor pptVinod Kumar
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVASURIT DATTA
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in javaGoogle
 

Was ist angesagt? (20)

Java threads
Java threadsJava threads
Java threads
 
Java package
Java packageJava package
Java package
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
Constructors in C++
Constructors in C++Constructors in C++
Constructors in C++
 
Abstraction in java
Abstraction in javaAbstraction in java
Abstraction in java
 
Life cycle-of-a-thread
Life cycle-of-a-threadLife cycle-of-a-thread
Life cycle-of-a-thread
 
I/O Streams
I/O StreamsI/O Streams
I/O Streams
 
Finalize() method
Finalize() methodFinalize() method
Finalize() method
 
Interface
InterfaceInterface
Interface
 
Java String Handling
Java String HandlingJava String Handling
Java String Handling
 
Applets in java
Applets in javaApplets in java
Applets in java
 
Jdbc architecture and driver types ppt
Jdbc architecture and driver types pptJdbc architecture and driver types ppt
Jdbc architecture and driver types ppt
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
Method overriding
Method overridingMethod overriding
Method overriding
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
 
Java program structure
Java program structure Java program structure
Java program structure
 
Applets
AppletsApplets
Applets
 
Files in c++
Files in c++Files in c++
Files in c++
 

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
 
Packages in java
Packages in javaPackages in java
Packages in javaAbhishek Khune
 
Java packages
Java packagesJava packages
Java packagesRaja Sekhar
 
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
 
Java exception
Java exception Java exception
Java exception Arati Gadgil
 
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
Java: ExceptionJava: Exception
Java: ExceptionTareq Hasan
 
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
 
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
 
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
 

Ă„hnlich wie Access Modifiers

27csharp
27csharp27csharp
27csharpSireesh K
 
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
 
[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access SpecifiersMuhammad Hammad Waseem
 
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
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and PolymorphismBG Java EE Course
 
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

Fundamentals
FundamentalsFundamentals
Fundamentalsmyrajendra
 
Data type
Data typeData type
Data typemyrajendra
 
Hibernate example1
Hibernate example1Hibernate example1
Hibernate example1myrajendra
 
Jdbc workflow
Jdbc workflowJdbc workflow
Jdbc workflowmyrajendra
 
2 jdbc drivers
2 jdbc drivers2 jdbc drivers
2 jdbc driversmyrajendra
 
3 jdbc api
3 jdbc api3 jdbc api
3 jdbc apimyrajendra
 
4 jdbc step1
4 jdbc step14 jdbc step1
4 jdbc step1myrajendra
 
Dao example
Dao exampleDao example
Dao examplemyrajendra
 
Sessionex1
Sessionex1Sessionex1
Sessionex1myrajendra
 
Internal
InternalInternal
Internalmyrajendra
 
3. elements
3. elements3. elements
3. elementsmyrajendra
 
2. attributes
2. attributes2. attributes
2. attributesmyrajendra
 
1 introduction to html
1 introduction to html1 introduction to html
1 introduction to htmlmyrajendra
 
Headings
HeadingsHeadings
Headingsmyrajendra
 
Starting jdbc
Starting jdbcStarting jdbc
Starting jdbcmyrajendra
 

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

call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 

KĂĽrzlich hochgeladen (20)

9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
CĂłdigo Creativo y Arte de Software | Unidad 1
CĂłdigo Creativo y Arte de Software | Unidad 1CĂłdigo Creativo y Arte de Software | Unidad 1
CĂłdigo Creativo y Arte de Software | Unidad 1
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 

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