Access ControlDhrubojyotiKayal
Access controlJava access specifiersAgenda
Mechanism to control how one class can use other classes – fields, methods and the class itselfThe Java access specifiers public, protected, and private are placed in front of each definition for each member in your class, whether it’s a field or a method.Each access specifier only controls the access for that particular definition. Access control
package / default publicprotectedprivateJava Access Specifiers
If you don’t provide an access specifier, it means “package access.” All the other classes in the current package have access to that member.To all the classes outside of this package, the member appears to be private – not accessibleclass A {}Package
Create two classes (A, B) in home.default packageCreate a class (C) in package home.training packageCreate an instance of A in BCreate an instance of A in CExercise
When you use the public keyword, it means that the member declaration that immediately follows public is available to everyone, in particular to the client programmer who uses the library. public
Create two public classes (A, B) in home.default packageCreate a class (C) in package home.training packageCreate an instance of A in BCreate an instance of A in CExercise
Skip over and revisit during inheritenceprotected
The private keyword means that no one can access that member except the class that contains that member, inside methods of that class Other classes in the same package cannot access private members, so it’s as if you’re even insulating the class against yourself Can be changed without worrying that other parts of the system will be affectedprivate void checkEven() {} private
Create a Java class X in pacakgetest.myprivateCreate another class Y in same package with two methods one public and one private with the public method invoking the private methodCreate an instance of Y in X in a method peekIn peek try to call the public and private methods of YExercise
Q&A

10 access control

  • 1.
  • 2.
  • 3.
    Mechanism to controlhow one class can use other classes – fields, methods and the class itselfThe Java access specifiers public, protected, and private are placed in front of each definition for each member in your class, whether it’s a field or a method.Each access specifier only controls the access for that particular definition. Access control
  • 4.
    package / defaultpublicprotectedprivateJava Access Specifiers
  • 5.
    If you don’tprovide an access specifier, it means “package access.” All the other classes in the current package have access to that member.To all the classes outside of this package, the member appears to be private – not accessibleclass A {}Package
  • 6.
    Create two classes(A, B) in home.default packageCreate a class (C) in package home.training packageCreate an instance of A in BCreate an instance of A in CExercise
  • 7.
    When you usethe public keyword, it means that the member declaration that immediately follows public is available to everyone, in particular to the client programmer who uses the library. public
  • 8.
    Create two publicclasses (A, B) in home.default packageCreate a class (C) in package home.training packageCreate an instance of A in BCreate an instance of A in CExercise
  • 9.
    Skip over andrevisit during inheritenceprotected
  • 10.
    The private keywordmeans that no one can access that member except the class that contains that member, inside methods of that class Other classes in the same package cannot access private members, so it’s as if you’re even insulating the class against yourself Can be changed without worrying that other parts of the system will be affectedprivate void checkEven() {} private
  • 11.
    Create a Javaclass X in pacakgetest.myprivateCreate another class Y in same package with two methods one public and one private with the public method invoking the private methodCreate an instance of Y in X in a method peekIn peek try to call the public and private methods of YExercise
  • 12.