SlideShare une entreprise Scribd logo
1  sur  12
Access Control DhrubojyotiKayal
Access control Java access specifiers Agenda
Mechanism to control how one class can use other classes – fields, methods and the class itself 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. Each access specifier only controls the access for that particular definition.  Access control
package / default  public protected private Java 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 accessible class A { } Package
Create two classes (A, B) in home.default package Create a class (C) in package home.training package Create an instance of A in B Create an instance of A in C Exercise
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 package Create a class (C) in package home.training package Create an instance of A in B Create an instance of A in C Exercise
Skip over and revisit during inheritence protected
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 affected private void checkEven() {}  private
Create a Java class X in pacakgetest.myprivate Create another class Y in same package with two methods one public and one private with the public method invoking the private method Create an instance of Y in X in a method peek In peek try to call the public and private methods of Y Exercise
Q&A

Contenu connexe

Tendances

Tendances (19)

Final presentation programming
Final presentation programmingFinal presentation programming
Final presentation programming
 
Lecture 10
Lecture 10Lecture 10
Lecture 10
 
Interface
InterfaceInterface
Interface
 
Inheritance
InheritanceInheritance
Inheritance
 
Java Inner Class
Java Inner ClassJava Inner Class
Java Inner Class
 
Interface in Java
Interface in JavaInterface in Java
Interface in Java
 
Lecture 8
Lecture 8Lecture 8
Lecture 8
 
Inner Classes in Java
Inner Classes in JavaInner Classes in Java
Inner Classes in Java
 
Access specifiers (Public Private Protected) C++
Access specifiers (Public Private  Protected) C++Access specifiers (Public Private  Protected) C++
Access specifiers (Public Private Protected) C++
 
Inner class
Inner classInner class
Inner class
 
Nested classes in java
Nested classes in javaNested classes in java
Nested classes in java
 
Access specifier
Access specifierAccess specifier
Access specifier
 
java tutorial 4
 java tutorial 4 java tutorial 4
java tutorial 4
 
Lect6
Lect6Lect6
Lect6
 
Understanding class, object & interface
Understanding class, object & interfaceUnderstanding class, object & interface
Understanding class, object & interface
 
Master of Computer Application (MCA) – Semester 4 MC0078
Master of Computer Application (MCA) – Semester 4  MC0078Master of Computer Application (MCA) – Semester 4  MC0078
Master of Computer Application (MCA) – Semester 4 MC0078
 
Inner class
Inner classInner class
Inner class
 
Class properties
Class propertiesClass properties
Class properties
 
Java interface
Java interface Java interface
Java interface
 

En vedette

المحاضرة الثامنة: تراكيب البيانات الطابور
المحاضرة الثامنة: تراكيب البيانات الطابورالمحاضرة الثامنة: تراكيب البيانات الطابور
المحاضرة الثامنة: تراكيب البيانات الطابور
Mahmoud Alfarra
 

En vedette (14)

MRM: PwC Top Issues
MRM:  PwC Top Issues  MRM:  PwC Top Issues
MRM: PwC Top Issues
 
Resume
ResumeResume
Resume
 
Schule
SchuleSchule
Schule
 
20160913 gondola cross border ecommerce
20160913 gondola cross border ecommerce20160913 gondola cross border ecommerce
20160913 gondola cross border ecommerce
 
Juridische tips voor cross border e-commerce naar Duitsland
Juridische tips voor cross border e-commerce naar DuitslandJuridische tips voor cross border e-commerce naar Duitsland
Juridische tips voor cross border e-commerce naar Duitsland
 
Hash code and equals
Hash code and equalsHash code and equals
Hash code and equals
 
Չղջիկ
ՉղջիկՉղջիկ
Չղջիկ
 
In depth: New financial instruments impairment model
In depth: New financial instruments impairment modelIn depth: New financial instruments impairment model
In depth: New financial instruments impairment model
 
Մի հին իրի պատմություն
Մի հին իրի պատմությունՄի հին իրի պատմություն
Մի հին իրի պատմություն
 
Theoretical study of weak intermolecular and intramolecular interactions
Theoretical study of weak intermolecular and intramolecular interactionsTheoretical study of weak intermolecular and intramolecular interactions
Theoretical study of weak intermolecular and intramolecular interactions
 
rimap Body of Knowledge
rimap Body of Knowledgerimap Body of Knowledge
rimap Body of Knowledge
 
Digital Agencies - Are You Aware of All The Ad Claims In Your Creatives?
Digital Agencies - Are You Aware of All The Ad Claims In Your Creatives?Digital Agencies - Are You Aware of All The Ad Claims In Your Creatives?
Digital Agencies - Are You Aware of All The Ad Claims In Your Creatives?
 
المحاضرة الثامنة: تراكيب البيانات الطابور
المحاضرة الثامنة: تراكيب البيانات الطابورالمحاضرة الثامنة: تراكيب البيانات الطابور
المحاضرة الثامنة: تراكيب البيانات الطابور
 
PwC Insurance -Stress-testing
PwC Insurance -Stress-testingPwC Insurance -Stress-testing
PwC Insurance -Stress-testing
 

Similaire à 10 access control

Similaire à 10 access control (20)

Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in java
 
Access Modifiers .pptx
Access Modifiers .pptxAccess Modifiers .pptx
Access Modifiers .pptx
 
Power point presentation on access specifier in OOPs
Power point presentation on access specifier in OOPsPower point presentation on access specifier in OOPs
Power point presentation on access specifier in OOPs
 
Session 11 - OOP's with Java - Packaging and Access Modifiers
Session 11 - OOP's with Java - Packaging and Access ModifiersSession 11 - OOP's with Java - Packaging and Access Modifiers
Session 11 - OOP's with Java - Packaging and Access Modifiers
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
 
Classes, objects, methods, constructors, this keyword in java
Classes, objects, methods, constructors, this keyword  in javaClasses, objects, methods, constructors, this keyword  in java
Classes, objects, methods, constructors, this keyword in java
 
OOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access Modifiers OOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access Modifiers
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
 
Lecture 9 access modifiers and packages
Lecture   9 access modifiers and packagesLecture   9 access modifiers and packages
Lecture 9 access modifiers and packages
 
Visibility Modifiers for Access Control.pptx
Visibility Modifiers for Access Control.pptxVisibility Modifiers for Access Control.pptx
Visibility Modifiers for Access Control.pptx
 
Packages access protection, importing packages
Packages   access protection, importing packagesPackages   access protection, importing packages
Packages access protection, importing packages
 
Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in java
 
OOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access ModifiersOOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access Modifiers
 
Inheritance and its types In Java
Inheritance and its types In JavaInheritance and its types In Java
Inheritance and its types In Java
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
 
7.Packages and Interfaces(MB).ppt .
7.Packages and Interfaces(MB).ppt             .7.Packages and Interfaces(MB).ppt             .
7.Packages and Interfaces(MB).ppt .
 
OOP_packages_222902019.pptx
OOP_packages_222902019.pptxOOP_packages_222902019.pptx
OOP_packages_222902019.pptx
 

Plus de dhrubo kayal

01 session tracking
01   session tracking01   session tracking
01 session tracking
dhrubo kayal
 
03 handling requests
03 handling requests03 handling requests
03 handling requests
dhrubo kayal
 
02 up close with servlets
02 up close with servlets02 up close with servlets
02 up close with servlets
dhrubo kayal
 
01 overview-servlets-and-environment-setup
01 overview-servlets-and-environment-setup01 overview-servlets-and-environment-setup
01 overview-servlets-and-environment-setup
dhrubo kayal
 
14 initialization & cleanup
14   initialization & cleanup14   initialization & cleanup
14 initialization & cleanup
dhrubo kayal
 
08 class and object
08   class and object08   class and object
08 class and object
dhrubo kayal
 
04 data types & variables
04   data types & variables04   data types & variables
04 data types & variables
dhrubo kayal
 
03 hello world with java
03   hello world with java03   hello world with java
03 hello world with java
dhrubo kayal
 

Plus de dhrubo kayal (20)

Cipla 20-09-2010
Cipla   20-09-2010Cipla   20-09-2010
Cipla 20-09-2010
 
01 session tracking
01   session tracking01   session tracking
01 session tracking
 
03 handling requests
03 handling requests03 handling requests
03 handling requests
 
02 up close with servlets
02 up close with servlets02 up close with servlets
02 up close with servlets
 
01 overview-servlets-and-environment-setup
01 overview-servlets-and-environment-setup01 overview-servlets-and-environment-setup
01 overview-servlets-and-environment-setup
 
19 reflection
19   reflection19   reflection
19 reflection
 
18 concurrency
18   concurrency18   concurrency
18 concurrency
 
17 exceptions
17   exceptions17   exceptions
17 exceptions
 
16 containers
16   containers16   containers
16 containers
 
15 interfaces
15   interfaces15   interfaces
15 interfaces
 
14 initialization & cleanup
14   initialization & cleanup14   initialization & cleanup
14 initialization & cleanup
 
13 inheritance
13   inheritance13   inheritance
13 inheritance
 
12 encapsulation
12   encapsulation12   encapsulation
12 encapsulation
 
11 static
11   static11   static
11 static
 
09 packages
09   packages09   packages
09 packages
 
08 class and object
08   class and object08   class and object
08 class and object
 
07 flow control
07   flow control07   flow control
07 flow control
 
05 operators
05   operators05   operators
05 operators
 
04 data types & variables
04   data types & variables04   data types & variables
04 data types & variables
 
03 hello world with java
03   hello world with java03   hello world with java
03 hello world with java
 

10 access control

  • 2. Access control Java access specifiers Agenda
  • 3. Mechanism to control how one class can use other classes – fields, methods and the class itself 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. Each access specifier only controls the access for that particular definition. Access control
  • 4. package / default public protected private Java Access Specifiers
  • 5. 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 accessible class A { } Package
  • 6. Create two classes (A, B) in home.default package Create a class (C) in package home.training package Create an instance of A in B Create an instance of A in C Exercise
  • 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. public
  • 8. Create two public classes (A, B) in home.default package Create a class (C) in package home.training package Create an instance of A in B Create an instance of A in C Exercise
  • 9. Skip over and revisit during inheritence protected
  • 10. 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 affected private void checkEven() {} private
  • 11. Create a Java class X in pacakgetest.myprivate Create another class Y in same package with two methods one public and one private with the public method invoking the private method Create an instance of Y in X in a method peek In peek try to call the public and private methods of Y Exercise
  • 12. Q&A