10 access control

105 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
105
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

10 access control

  1. 1. Access Control<br />DhrubojyotiKayal<br />
  2. 2. Access control<br />Java access specifiers<br />Agenda<br />
  3. 3. Mechanism to control how one class can use other classes – fields, methods and the class itself<br />The 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.<br />Each access specifier only controls the access for that particular definition. <br />Access control <br />
  4. 4. package / default <br />public<br />protected<br />private<br />Java Access Specifiers<br />
  5. 5. If you don’t provide an access specifier, it means “package access.” <br />All the other classes in the current package have access to that member.<br />To all the classes outside of this package, the member appears to be private – not accessible<br />class A {<br />}<br />Package<br />
  6. 6. Create two classes (A, B) in home.default package<br />Create a class (C) in package home.training package<br />Create an instance of A in B<br />Create an instance of A in C<br />Exercise<br />
  7. 7. 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. <br />public <br />
  8. 8. Create two public classes (A, B) in home.default package<br />Create a class (C) in package home.training package<br />Create an instance of A in B<br />Create an instance of A in C<br />Exercise<br />
  9. 9. Skip over and revisit during inheritence<br />protected<br />
  10. 10. The private keyword means that no one can access that member except the class that contains that member, inside methods of that class <br />Other classes in the same package cannot access private members, so it’s as if you’re even insulating the class against yourself <br />Can be changed without worrying that other parts of the system will be affected<br />private void checkEven() {} <br />private<br />
  11. 11. Create a Java class X in pacakgetest.myprivate<br />Create another class Y in same package with two methods one public and one private with the public method invoking the private method<br />Create an instance of Y in X in a method peek<br />In peek try to call the public and private methods of Y<br />Exercise<br />
  12. 12. Q&A<br />

×