SlideShare a Scribd company logo
1 of 14
PowerPoint Presentation on
Access Specifier in
OOPs(Java Programming
Language)
COURSE NAME: OBJECT ORIENTED PROGRAMMING
COURSE CODE:
Introducing Access Control
Encapsulation links data with the code that manipulates it.
Through encapsulation, we can control what part of a program can access the members of a
class.
By controlling access, we can prevent misuse.
How a member can be accessed is determined by access specifiers that modifies its
declaration.
What is Access specifiers?
Access specifiers (or access modifiers) are keywords in object-oriented languages that set the
accessibility of classes, methods, and other members.
Access modifiers in object oriented programming language:
 C++ uses only three access modifiers called public, protected and private.
 C# extends that number to six. It’s modifiers are public, protected, internal, private, protected internal
and private protected.
 While Java has four access modifiers i.e. public, protected, private and package.
The access modifier package is the default and used, if any other modifier keyword is missing.
 The meaning of these access specifiers may differ from one language to another.
Access control in JAVA
Java provides many levels of protection to allow fine-grained control over the visibility of
variables and methods within classes, subclasses and packages.
Classes and packages are both means of encapsulating and containing the name space and
scope of variables and methods.
Because of the interplay between classes and package, Java addresses four categories of
visibility for class members.
 Subclasses in the same package
 Non-subclasses in the same package
 Subclasses in different packages
 Classes that are neither in the same package nor subclasses
The three specifiers public, private and protected, provide a variety of ways to produce the
many levels of access required by these category.
Access modifiers in JAVA
Let’s have a look on these access modifiers.
Anything declared public can be accessed from anywhere.
Anything declared private cannot be seen outside of its class.
If we want to allow an element to be seen outside current package, but only to classes that
subclass our class directly, then declare that element protected.
When a member does not have an explicit access specification, it is visible to subclasses as well
as to other classes in the same package. This is the default access.
Class Member Access Table
Private No Modifier Protected Public
Same Class Yes Yes Yes Yes
Same package
subclass
No Yes Yes Yes
Same package
non-subclass
No Yes Yes Yes
Different package
subclass
No No Yes Yes
Different package
non-subclass
No No No Yes
Examples
The following example shows all combinations of the access modifiers.
This example uses two packages(p1 and p2) and five classes.
 This is file Protection.java:
package p1;
public class Protection {
int n = 1;
private int n_pri = 2;
protected int n_pro = 3;
public int n_pub = 4;
public Protection()
{
System.out.println("base constructor");
System.out.println("n = " + n);
System.out.println("private variable = " + n_pri);
System.out.println("protected variable = " + n_pro);
System.out.println("public variable = " + n_pub);
}
}
 This is file Derived.java:
package p1;
public class Derived extends Protection{
public Derived()
{
System.out.println("derived constructor");
System.out.println("n = " + n);
// Private variable - class only
// System.out.println("private variable = " + n_pri);
System.out.println("protected variable = " + n_pro);
System.out.println("public variable = " + n_pub);
}
}
 This is file SamePackage.java:
package p1;
public class SamePackage {
public SamePackage()
{
Protection p = new Protection();
System.out.println("SamePackage NonSubclass constructor");
System.out.println("n = " + p.n);
// private variable - class only
// System.out.println("private variable = " + p.n_pri);
System.out.println("protected variable = " + p.n_pro);
System.out.println("public variable = " + p.n_pub);
}
}
Following is the source code for other package, p2.
This is file Protection2.java:
package p2;
public class Protection2 extends p1.Protection{
public Protection2()
{
System.out.println("Different Package subclass");
// No modifier - class or same package only
// System.out.println("n = " + n);
// private variable - class only
// System.out.println("private variable = " + n_pri);
System.out.println("protected vaiable = " + n_pro);
System.out.println("public variabl = " + n_pub);
}
}
 This is file OtherPackage.java:
package p2;
public class OtherPackage {
public OtherPackage()
{
p1.Protection p = new p1.Protection();
System.out.println("Other Package non sublcass");
// no modifier -class or same package only
// System.out.println("n = " + p.n);
// private variable - class only
// System.out.println("private variable = " + p.n_pri);
// protected variable - class, subclass or same package only
// System.out.println("protected variable = " + p.n_pro);
System.out.println("public variable = " + p.n_pub);
}
}
We have two test files. The one is for package p1 is shown here:
package p1;
public class DemoTest1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Protection obj = new Protection();
Derived obj1 = new Derived();
SamePackage obj2 = new SamePackage();
}
}
 The test file for p2 is shown next:
package p2;
public class DemoTest2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Protection2 obj3 = new Protection2();
OtherPackage obj4 = new OtherPackage();
}
}

More Related Content

What's hot

Object Oriented Programming Concepts for beginners
Object Oriented Programming Concepts for beginners Object Oriented Programming Concepts for beginners
Object Oriented Programming Concepts for beginners Vibhawa Nirmal
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Vineeta Garg
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oopsHirra Sultan
 
6. Integrity and Security in DBMS
6. Integrity and Security in DBMS6. Integrity and Security in DBMS
6. Integrity and Security in DBMSkoolkampus
 
[OOP - Lec 08] Encapsulation (Information Hiding)
[OOP - Lec 08] Encapsulation (Information Hiding)[OOP - Lec 08] Encapsulation (Information Hiding)
[OOP - Lec 08] Encapsulation (Information Hiding)Muhammad Hammad Waseem
 
Exception handling in plsql
Exception handling in plsqlException handling in plsql
Exception handling in plsqlArun Sial
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++rprajat007
 
C++ OOPS Concept
C++ OOPS ConceptC++ OOPS Concept
C++ OOPS ConceptBoopathi K
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++Laxman Puri
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Michelle Anne Meralpis
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance pptNivegeetha
 

What's hot (20)

interface in c#
interface in c#interface in c#
interface in c#
 
Object Oriented Programming Concepts for beginners
Object Oriented Programming Concepts for beginners Object Oriented Programming Concepts for beginners
Object Oriented Programming Concepts for beginners
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oops
 
6. Integrity and Security in DBMS
6. Integrity and Security in DBMS6. Integrity and Security in DBMS
6. Integrity and Security in DBMS
 
[OOP - Lec 08] Encapsulation (Information Hiding)
[OOP - Lec 08] Encapsulation (Information Hiding)[OOP - Lec 08] Encapsulation (Information Hiding)
[OOP - Lec 08] Encapsulation (Information Hiding)
 
Exception handling in plsql
Exception handling in plsqlException handling in plsql
Exception handling in plsql
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
ASP.NET Web form
ASP.NET Web formASP.NET Web form
ASP.NET Web form
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
 
C++ OOPS Concept
C++ OOPS ConceptC++ OOPS Concept
C++ OOPS Concept
 
Chapter2 Encapsulation (Java)
Chapter2 Encapsulation (Java)Chapter2 Encapsulation (Java)
Chapter2 Encapsulation (Java)
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Inheritance C#
Inheritance C#Inheritance C#
Inheritance C#
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance ppt
 
Java notes jkuat it
Java notes jkuat itJava notes jkuat it
Java notes jkuat it
 
Single inheritance
Single inheritanceSingle inheritance
Single inheritance
 
Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
 
Interface in java
Interface in javaInterface in java
Interface in java
 

Similar to Power point presentation on access specifier in OOPs

Visibility Modifiers for Access Control.pptx
Visibility Modifiers for Access Control.pptxVisibility Modifiers for Access Control.pptx
Visibility Modifiers for Access Control.pptxnaazminshaikh1727
 
Java - Packages Concepts
Java - Packages ConceptsJava - Packages Concepts
Java - Packages ConceptsVicter Paul
 
Packages access protection, importing packages
Packages   access protection, importing packagesPackages   access protection, importing packages
Packages access protection, importing packagesTharuniDiddekunta
 
Lecture 9 access modifiers and packages
Lecture   9 access modifiers and packagesLecture   9 access modifiers and packages
Lecture 9 access modifiers and packagesmanish kumar
 
Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in javaAshwin Thadani
 
7.Packages and Interfaces(MB).ppt .
7.Packages and Interfaces(MB).ppt             .7.Packages and Interfaces(MB).ppt             .
7.Packages and Interfaces(MB).ppt .happycocoman
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in javaVishnu Suresh
 
Class notes(week 7) on packages
Class notes(week 7) on packagesClass notes(week 7) on packages
Class notes(week 7) on packagesKuntal Bhowmick
 
Unit4 java
Unit4 javaUnit4 java
Unit4 javamrecedu
 
Java package
Java packageJava package
Java packageCS_GDRCST
 

Similar to Power point presentation on access specifier in OOPs (20)

Visibility Modifiers for Access Control.pptx
Visibility Modifiers for Access Control.pptxVisibility Modifiers for Access Control.pptx
Visibility Modifiers for Access Control.pptx
 
Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in java
 
Java - Packages Concepts
Java - Packages ConceptsJava - Packages Concepts
Java - Packages Concepts
 
150950107056 2150704
150950107056 2150704150950107056 2150704
150950107056 2150704
 
Lecture 19
Lecture 19Lecture 19
Lecture 19
 
Unit 4 Java
Unit 4 JavaUnit 4 Java
Unit 4 Java
 
Packages
PackagesPackages
Packages
 
Unit 2 notes.pdf
Unit 2 notes.pdfUnit 2 notes.pdf
Unit 2 notes.pdf
 
Packages access protection, importing packages
Packages   access protection, importing packagesPackages   access protection, importing packages
Packages access protection, importing packages
 
Lecture 9 access modifiers and packages
Lecture   9 access modifiers and packagesLecture   9 access modifiers and packages
Lecture 9 access modifiers and packages
 
Introduction to package in java
Introduction to package in javaIntroduction to package in java
Introduction to package in java
 
Packages and interfaces
Packages and interfacesPackages and interfaces
Packages and interfaces
 
Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in java
 
7.Packages and Interfaces(MB).ppt .
7.Packages and Interfaces(MB).ppt             .7.Packages and Interfaces(MB).ppt             .
7.Packages and Interfaces(MB).ppt .
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
 
Class notes(week 7) on packages
Class notes(week 7) on packagesClass notes(week 7) on packages
Class notes(week 7) on packages
 
Unit4 java
Unit4 javaUnit4 java
Unit4 java
 
Java package
Java packageJava package
Java package
 
Packages and interface
Packages and interfacePackages and interface
Packages and interface
 
Java packages oop
Java packages oopJava packages oop
Java packages oop
 

Recently uploaded

What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...RajaP95
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAbhinavSharma374939
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 

Recently uploaded (20)

What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog Converter
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 

Power point presentation on access specifier in OOPs

  • 1. PowerPoint Presentation on Access Specifier in OOPs(Java Programming Language) COURSE NAME: OBJECT ORIENTED PROGRAMMING COURSE CODE:
  • 2. Introducing Access Control Encapsulation links data with the code that manipulates it. Through encapsulation, we can control what part of a program can access the members of a class. By controlling access, we can prevent misuse. How a member can be accessed is determined by access specifiers that modifies its declaration.
  • 3. What is Access specifiers? Access specifiers (or access modifiers) are keywords in object-oriented languages that set the accessibility of classes, methods, and other members. Access modifiers in object oriented programming language:  C++ uses only three access modifiers called public, protected and private.  C# extends that number to six. It’s modifiers are public, protected, internal, private, protected internal and private protected.  While Java has four access modifiers i.e. public, protected, private and package. The access modifier package is the default and used, if any other modifier keyword is missing.  The meaning of these access specifiers may differ from one language to another.
  • 4. Access control in JAVA Java provides many levels of protection to allow fine-grained control over the visibility of variables and methods within classes, subclasses and packages. Classes and packages are both means of encapsulating and containing the name space and scope of variables and methods. Because of the interplay between classes and package, Java addresses four categories of visibility for class members.  Subclasses in the same package  Non-subclasses in the same package  Subclasses in different packages  Classes that are neither in the same package nor subclasses The three specifiers public, private and protected, provide a variety of ways to produce the many levels of access required by these category.
  • 5. Access modifiers in JAVA Let’s have a look on these access modifiers. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. If we want to allow an element to be seen outside current package, but only to classes that subclass our class directly, then declare that element protected. When a member does not have an explicit access specification, it is visible to subclasses as well as to other classes in the same package. This is the default access.
  • 6. Class Member Access Table Private No Modifier Protected Public Same Class Yes Yes Yes Yes Same package subclass No Yes Yes Yes Same package non-subclass No Yes Yes Yes Different package subclass No No Yes Yes Different package non-subclass No No No Yes
  • 7. Examples The following example shows all combinations of the access modifiers. This example uses two packages(p1 and p2) and five classes.
  • 8.  This is file Protection.java: package p1; public class Protection { int n = 1; private int n_pri = 2; protected int n_pro = 3; public int n_pub = 4; public Protection() { System.out.println("base constructor"); System.out.println("n = " + n); System.out.println("private variable = " + n_pri); System.out.println("protected variable = " + n_pro); System.out.println("public variable = " + n_pub); } }
  • 9.  This is file Derived.java: package p1; public class Derived extends Protection{ public Derived() { System.out.println("derived constructor"); System.out.println("n = " + n); // Private variable - class only // System.out.println("private variable = " + n_pri); System.out.println("protected variable = " + n_pro); System.out.println("public variable = " + n_pub); } }
  • 10.  This is file SamePackage.java: package p1; public class SamePackage { public SamePackage() { Protection p = new Protection(); System.out.println("SamePackage NonSubclass constructor"); System.out.println("n = " + p.n); // private variable - class only // System.out.println("private variable = " + p.n_pri); System.out.println("protected variable = " + p.n_pro); System.out.println("public variable = " + p.n_pub); } }
  • 11. Following is the source code for other package, p2. This is file Protection2.java: package p2; public class Protection2 extends p1.Protection{ public Protection2() { System.out.println("Different Package subclass"); // No modifier - class or same package only // System.out.println("n = " + n); // private variable - class only // System.out.println("private variable = " + n_pri); System.out.println("protected vaiable = " + n_pro); System.out.println("public variabl = " + n_pub); } }
  • 12.  This is file OtherPackage.java: package p2; public class OtherPackage { public OtherPackage() { p1.Protection p = new p1.Protection(); System.out.println("Other Package non sublcass"); // no modifier -class or same package only // System.out.println("n = " + p.n); // private variable - class only // System.out.println("private variable = " + p.n_pri); // protected variable - class, subclass or same package only // System.out.println("protected variable = " + p.n_pro); System.out.println("public variable = " + p.n_pub); } }
  • 13. We have two test files. The one is for package p1 is shown here: package p1; public class DemoTest1 { public static void main(String[] args) { // TODO Auto-generated method stub Protection obj = new Protection(); Derived obj1 = new Derived(); SamePackage obj2 = new SamePackage(); } }
  • 14.  The test file for p2 is shown next: package p2; public class DemoTest2 { public static void main(String[] args) { // TODO Auto-generated method stub Protection2 obj3 = new Protection2(); OtherPackage obj4 = new OtherPackage(); } }