SlideShare a Scribd company logo
3
Most read
5
Most read
10
Most read
JAVA
Topics for Today’s Session
Access Modifiers
Access Modifiers in Java
 In Java, access modifiers are used to set the accessibility
(visibility) of classes, interfaces, variables, methods,
constructors and data members.
 As the name suggests access modifiers in Java helps to
restrict the scope of a class, constructor , variable ,
method or data member.
 There are four types of access modifiers available in
java:
1. Default – No keyword required
2. Private
3. Protected
4. Public
nChild class
inside
same package
Child class
inside
same
folder but
different
package
protected
private
public
default
Source Folder
Java package
public can be
accessed
anywhere
inheritsinherits
 If you do not specify any access level, it will be the
default.
 The data members, class or methods which are not
declared using any access modifiers i.e. having default
access modifier are accessible only within the same
package.
 The access level of a default modifier is only within the
package.
 It cannot be accessed from outside the package.
 Default access modifier means we do not explicitly
declare an access modifier for a class, field, method, etc.
//Java program to illustrate default modifier
package p1;
//Class Hello is having Default access modifier
class Hello {
void display() {
System.out.println("Hello World!");
} }
// program to illustrate error while using class from different package with
//default modifier
package p2;
import p1.*;
//This class is having default access modifier
class HelloNew {
public static void main(String args[]) {
//accessing class Hello from package p1
Hello obj = new Hello();
obj.display();
} }
Output:
Compile time error
 The private access modifier is specified using the
keyword private.
 The access level of a private modifier is only within the
class. It cannot be accessed from outside the class
 The methods or data members declared as private are
accessible only within the class in which they are
declared.
 Any other class of same package will not be able to
access these members.
 These cannot be inherited by subclasses and therefore
not accessible in subclasses.
 We can not override private members.
Example: private int x;
/*Pprogram to illustrate error while using class from different package
with private modifier */
package p1;
class A {
private void display() {
System.out.println("Hello Prathibha");
} }
class B {
public static void main(String args[]) {
A obj = new A();
//trying to access private method of another class
obj.display();
} }
Output:
error: display() has private access in A
obj.display();
//program accessing private members
class HelloData {
private String name; // private variable
}
class HlloMain {
public static void main(String[] main){
HelloData d = new HelloData(); // create an object of
HelloData
// access private variable and field from another class
d.name = "Prathibha Virtual Classes";
}
}
When we run the program, we will get the following error:
HelloMain.java:18: error: name has private access in HelloData
d.name = "Prathibha Virtual Classes”;
Protected Access Modifier
 A variable or method can be declared as protected using a
keyword ‘protected’.
 These can be accessed within the same package as well as
from subclasses.
 This is specially designed for supporting Inheritance.
 Protected access is used to access the variables or methods
visible everywhere in the same package in which it is
defined and also in the subclasses of other packages.
 Non subclasses of other packages cannot access protected
members.
Example: protected int x;
class Animal {
protected void display() { // protected method
System.out.println("I am an animal");
} }
class Dog extends Animal {
public static void main(String[] args) {
// create an object of Dog class
Dog dog = new Dog();
// access protected method
dog.display();
} }
Output:
I am an animal
//Program to illustrate protected modifier
package p1;
class A {
protected void display() {
System.out.println("Prathibha Virtual Classes");
} }
//Program to illustrate protected modifier
package p2;
import p1.*; //importing all classes in package p1
class B extends A { //Class B is subclass of A
public static void main(String args[]) {
B obj = new B();
obj.display();
} }
Output:
Prathibha Virtual Classes
 A variable or method can be declared as public using a
keyword ‘public’.
 The public access modifier has the widest scope among all
other access modifiers.
 Classes, methods or data members which are declared as
public are accessible from every where in the program. There
is no restriction on the scope of a public data members.
 The public methods or variables can be accessed within the
class and in other classes (outside) using objects and in all
packages.
Example: public int n;
public void sum ( ) {…}
//Java program to illustrate public modifier
package p1;
public class A {
public void display() {
System.out.println("Prathibha Virtual Classes");
}
}
package p2;
import p1.*;
class B {
public static void main(String args[]) {
A obj = new A;
obj.display();
}
}
Output:
Prathibha Virtual Classes
// Animal.java file public class
public class Animal {
public int legCount;
public void display() { // public method
System.out.println("I am an animal.");
System.out.println("I have " + legCount + " legs.");
}}
// Main.java
public class Main {
public static void main( String[] args ) {
Animal ani = new Animal(); // accessing the public class
ani.legCount = 4; // accessing the public variable
ani.display(); // accessing the public method
} }
Output:
I am an animal.
I have 4 legs.
Private protected
 We may declare a member as “private protected”.
 This is used to access the member in all subclasses in all
packages.
 This is accessed in its own class and all the subclasses of
all the packages. But it is not accessed in other classes in
the same package and non sub classes in the other
packages.
Example private protected int x;
Access Modifier Visibility / Accessibility
public Visible everywhere in the program
friendly(default) Visible everywhere in the current package.
protected Visible everywhere in the current package and sub
classes in other packages
private Visible only in the class in which it is defined
private protected Visible in its own class and all the sub class of all the
packages
Accessibility of member using various
Access / Visibility modifiers:


Access modifiers in java

More Related Content

What's hot (20)

PPTX
Super Keyword in Java.pptx
KrutikaWankhade1
 
PPTX
java interface and packages
VINOTH R
 
PPT
Strings
naslin prestilda
 
PPTX
Static Members-Java.pptx
ADDAGIRIVENKATARAVIC
 
PPTX
Java Method, Static Block
Infoviaan Technologies
 
PPTX
Arrays in java
bhavesh prakash
 
PPTX
Java Data Types
Spotle.ai
 
PPTX
Packages,static,this keyword in java
Vishnu Suresh
 
PPS
String and string buffer
kamal kotecha
 
PPTX
I/O Streams
Ravi Chythanya
 
PDF
Class and Objects in Java
Spotle.ai
 
PPTX
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
PPTX
Constructor in java
Pavith Gunasekara
 
PPT
Class and object in C++
rprajat007
 
PPT
Java interfaces
Raja Sekhar
 
PPT
Java access modifiers
Srinivas Reddy
 
PPTX
Static keyword ppt
Vinod Kumar
 
PPTX
This keyword in java
Hitesh Kumar
 
PPTX
History Of JAVA
ARSLANAHMED107
 
PPTX
Type casting in java
Farooq Baloch
 
Super Keyword in Java.pptx
KrutikaWankhade1
 
java interface and packages
VINOTH R
 
Static Members-Java.pptx
ADDAGIRIVENKATARAVIC
 
Java Method, Static Block
Infoviaan Technologies
 
Arrays in java
bhavesh prakash
 
Java Data Types
Spotle.ai
 
Packages,static,this keyword in java
Vishnu Suresh
 
String and string buffer
kamal kotecha
 
I/O Streams
Ravi Chythanya
 
Class and Objects in Java
Spotle.ai
 
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
Constructor in java
Pavith Gunasekara
 
Class and object in C++
rprajat007
 
Java interfaces
Raja Sekhar
 
Java access modifiers
Srinivas Reddy
 
Static keyword ppt
Vinod Kumar
 
This keyword in java
Hitesh Kumar
 
History Of JAVA
ARSLANAHMED107
 
Type casting in java
Farooq Baloch
 

Similar to Access modifiers in java (20)

PPTX
Access Modifiers of oop classes concept.pptx
umarshafi527
 
PPTX
Access Modifiers .pptx
MDRakibKhan3
 
PPTX
Visibility Modifiers for Access Control.pptx
naazminshaikh1727
 
PPTX
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_03-Mar-2021_L13...
MaruMengesha
 
PPTX
Java access modifiers
Khaled Adnan
 
PPTX
Lect6
Jamsher bhanbhro
 
PPTX
Power point presentation on access specifier in OOPs
AdrizaBera
 
PPTX
Access modifiers in java
Sourabrata Mukherjee
 
PPTX
Access modifiers in java
Ashwin Thadani
 
PDF
CHAPTER 3 part1.pdf
FacultyAnupamaAlagan
 
PPTX
Imp Key.pptx very easy to learn go for it
dwivedyp
 
PPTX
Access modifier and inheritance
Dikshyanta Dhungana
 
PDF
Access modifiers in java
Muthukumaran Subramanian
 
PDF
Access specifiers (Public Private Protected) C++
vivekkumar2938
 
PDF
Access Specifier and encapusulation.pdf
SunithaKrishnan9
 
PPTX
C# Access modifiers
Prem Kumar Badri
 
PPTX
Std 12 computer chapter 8 classes and object in java (part 2)
Nuzhat Memon
 
PPT
Access Protection
myrajendra
 
PPTX
Access Modifiers in Java.pptx
sarthakgithub
 
PDF
Java modifiers
Soba Arjun
 
Access Modifiers of oop classes concept.pptx
umarshafi527
 
Access Modifiers .pptx
MDRakibKhan3
 
Visibility Modifiers for Access Control.pptx
naazminshaikh1727
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_03-Mar-2021_L13...
MaruMengesha
 
Java access modifiers
Khaled Adnan
 
Power point presentation on access specifier in OOPs
AdrizaBera
 
Access modifiers in java
Sourabrata Mukherjee
 
Access modifiers in java
Ashwin Thadani
 
CHAPTER 3 part1.pdf
FacultyAnupamaAlagan
 
Imp Key.pptx very easy to learn go for it
dwivedyp
 
Access modifier and inheritance
Dikshyanta Dhungana
 
Access modifiers in java
Muthukumaran Subramanian
 
Access specifiers (Public Private Protected) C++
vivekkumar2938
 
Access Specifier and encapusulation.pdf
SunithaKrishnan9
 
C# Access modifiers
Prem Kumar Badri
 
Std 12 computer chapter 8 classes and object in java (part 2)
Nuzhat Memon
 
Access Protection
myrajendra
 
Access Modifiers in Java.pptx
sarthakgithub
 
Java modifiers
Soba Arjun
 
Ad

More from Madishetty Prathibha (14)

PPTX
Object Oriented programming - Introduction
Madishetty Prathibha
 
PPTX
Constructor in java
Madishetty Prathibha
 
PPTX
Control statements in java
Madishetty Prathibha
 
PPTX
Classes objects in java
Madishetty Prathibha
 
PPTX
Structure of java program diff c- cpp and java
Madishetty Prathibha
 
PPTX
Operators in java
Madishetty Prathibha
 
PPTX
Types of datastructures
Madishetty Prathibha
 
PPTX
Introduction to algorithms
Madishetty Prathibha
 
PDF
Java data types, variables and jvm
Madishetty Prathibha
 
PPTX
Introduction to data structures (ss)
Madishetty Prathibha
 
PPTX
Java Tokens
Madishetty Prathibha
 
PDF
Oops concepts || Object Oriented Programming Concepts in Java
Madishetty Prathibha
 
PPT
Java features
Madishetty Prathibha
 
PPSX
Introduction of java
Madishetty Prathibha
 
Object Oriented programming - Introduction
Madishetty Prathibha
 
Constructor in java
Madishetty Prathibha
 
Control statements in java
Madishetty Prathibha
 
Classes objects in java
Madishetty Prathibha
 
Structure of java program diff c- cpp and java
Madishetty Prathibha
 
Operators in java
Madishetty Prathibha
 
Types of datastructures
Madishetty Prathibha
 
Introduction to algorithms
Madishetty Prathibha
 
Java data types, variables and jvm
Madishetty Prathibha
 
Introduction to data structures (ss)
Madishetty Prathibha
 
Oops concepts || Object Oriented Programming Concepts in Java
Madishetty Prathibha
 
Java features
Madishetty Prathibha
 
Introduction of java
Madishetty Prathibha
 
Ad

Recently uploaded (20)

PDF
Council of Chalcedon Re-Examined
Smiling Lungs
 
PDF
Vietnam Street Food & QSR Market 2025-1.pdf
ssuserec8cd0
 
PPTX
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PDF
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PPTX
AIMA UCSC-SV Leadership_in_the_AI_era 20250628 v16.pptx
home
 
PPTX
SD_GMRC5_Session 6AB_Dulog Pedagohikal at Pagtataya (1).pptx
NickeyArguelles
 
PDF
I3PM Case study smart parking 2025 with uptoIP® and ABP
MIPLM
 
PPTX
How to Manage Expiry Date in Odoo 18 Inventory
Celine George
 
PPTX
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
PPTX
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
PDF
AI-assisted IP-Design lecture from the MIPLM 2025
MIPLM
 
PPTX
PLANNING FOR EMERGENCY AND DISASTER MANAGEMENT ppt.pptx
PRADEEP ABOTHU
 
PPTX
Controller Request and Response in Odoo18
Celine George
 
PDF
WATERSHED MANAGEMENT CASE STUDIES - ULUGURU MOUNTAINS AND ARVARI RIVERpdf
Ar.Asna
 
PDF
Lean IP - Lecture by Dr Oliver Baldus at the MIPLM 2025
MIPLM
 
PPTX
Difference between write and update in odoo 18
Celine George
 
PPTX
ENG8_Q1_WEEK2_LESSON1. Presentation pptx
marawehsvinetshe
 
PPTX
Ward Management: Patient Care, Personnel, Equipment, and Environment.pptx
PRADEEP ABOTHU
 
PPTX
Different types of inheritance in odoo 18
Celine George
 
Council of Chalcedon Re-Examined
Smiling Lungs
 
Vietnam Street Food & QSR Market 2025-1.pdf
ssuserec8cd0
 
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
AIMA UCSC-SV Leadership_in_the_AI_era 20250628 v16.pptx
home
 
SD_GMRC5_Session 6AB_Dulog Pedagohikal at Pagtataya (1).pptx
NickeyArguelles
 
I3PM Case study smart parking 2025 with uptoIP® and ABP
MIPLM
 
How to Manage Expiry Date in Odoo 18 Inventory
Celine George
 
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
AI-assisted IP-Design lecture from the MIPLM 2025
MIPLM
 
PLANNING FOR EMERGENCY AND DISASTER MANAGEMENT ppt.pptx
PRADEEP ABOTHU
 
Controller Request and Response in Odoo18
Celine George
 
WATERSHED MANAGEMENT CASE STUDIES - ULUGURU MOUNTAINS AND ARVARI RIVERpdf
Ar.Asna
 
Lean IP - Lecture by Dr Oliver Baldus at the MIPLM 2025
MIPLM
 
Difference between write and update in odoo 18
Celine George
 
ENG8_Q1_WEEK2_LESSON1. Presentation pptx
marawehsvinetshe
 
Ward Management: Patient Care, Personnel, Equipment, and Environment.pptx
PRADEEP ABOTHU
 
Different types of inheritance in odoo 18
Celine George
 

Access modifiers in java

  • 2. Topics for Today’s Session Access Modifiers
  • 3. Access Modifiers in Java  In Java, access modifiers are used to set the accessibility (visibility) of classes, interfaces, variables, methods, constructors and data members.  As the name suggests access modifiers in Java helps to restrict the scope of a class, constructor , variable , method or data member.  There are four types of access modifiers available in java: 1. Default – No keyword required 2. Private 3. Protected 4. Public
  • 4. nChild class inside same package Child class inside same folder but different package protected private public default Source Folder Java package public can be accessed anywhere inheritsinherits
  • 5.  If you do not specify any access level, it will be the default.  The data members, class or methods which are not declared using any access modifiers i.e. having default access modifier are accessible only within the same package.  The access level of a default modifier is only within the package.  It cannot be accessed from outside the package.  Default access modifier means we do not explicitly declare an access modifier for a class, field, method, etc.
  • 6. //Java program to illustrate default modifier package p1; //Class Hello is having Default access modifier class Hello { void display() { System.out.println("Hello World!"); } } // program to illustrate error while using class from different package with //default modifier package p2; import p1.*; //This class is having default access modifier class HelloNew { public static void main(String args[]) { //accessing class Hello from package p1 Hello obj = new Hello(); obj.display(); } } Output: Compile time error
  • 7.  The private access modifier is specified using the keyword private.  The access level of a private modifier is only within the class. It cannot be accessed from outside the class  The methods or data members declared as private are accessible only within the class in which they are declared.  Any other class of same package will not be able to access these members.  These cannot be inherited by subclasses and therefore not accessible in subclasses.  We can not override private members. Example: private int x;
  • 8. /*Pprogram to illustrate error while using class from different package with private modifier */ package p1; class A { private void display() { System.out.println("Hello Prathibha"); } } class B { public static void main(String args[]) { A obj = new A(); //trying to access private method of another class obj.display(); } } Output: error: display() has private access in A obj.display();
  • 9. //program accessing private members class HelloData { private String name; // private variable } class HlloMain { public static void main(String[] main){ HelloData d = new HelloData(); // create an object of HelloData // access private variable and field from another class d.name = "Prathibha Virtual Classes"; } } When we run the program, we will get the following error: HelloMain.java:18: error: name has private access in HelloData d.name = "Prathibha Virtual Classes”;
  • 10. Protected Access Modifier  A variable or method can be declared as protected using a keyword ‘protected’.  These can be accessed within the same package as well as from subclasses.  This is specially designed for supporting Inheritance.  Protected access is used to access the variables or methods visible everywhere in the same package in which it is defined and also in the subclasses of other packages.  Non subclasses of other packages cannot access protected members. Example: protected int x;
  • 11. class Animal { protected void display() { // protected method System.out.println("I am an animal"); } } class Dog extends Animal { public static void main(String[] args) { // create an object of Dog class Dog dog = new Dog(); // access protected method dog.display(); } } Output: I am an animal
  • 12. //Program to illustrate protected modifier package p1; class A { protected void display() { System.out.println("Prathibha Virtual Classes"); } } //Program to illustrate protected modifier package p2; import p1.*; //importing all classes in package p1 class B extends A { //Class B is subclass of A public static void main(String args[]) { B obj = new B(); obj.display(); } } Output: Prathibha Virtual Classes
  • 13.  A variable or method can be declared as public using a keyword ‘public’.  The public access modifier has the widest scope among all other access modifiers.  Classes, methods or data members which are declared as public are accessible from every where in the program. There is no restriction on the scope of a public data members.  The public methods or variables can be accessed within the class and in other classes (outside) using objects and in all packages. Example: public int n; public void sum ( ) {…}
  • 14. //Java program to illustrate public modifier package p1; public class A { public void display() { System.out.println("Prathibha Virtual Classes"); } } package p2; import p1.*; class B { public static void main(String args[]) { A obj = new A; obj.display(); } } Output: Prathibha Virtual Classes
  • 15. // Animal.java file public class public class Animal { public int legCount; public void display() { // public method System.out.println("I am an animal."); System.out.println("I have " + legCount + " legs."); }} // Main.java public class Main { public static void main( String[] args ) { Animal ani = new Animal(); // accessing the public class ani.legCount = 4; // accessing the public variable ani.display(); // accessing the public method } } Output: I am an animal. I have 4 legs.
  • 16. Private protected  We may declare a member as “private protected”.  This is used to access the member in all subclasses in all packages.  This is accessed in its own class and all the subclasses of all the packages. But it is not accessed in other classes in the same package and non sub classes in the other packages. Example private protected int x;
  • 17. Access Modifier Visibility / Accessibility public Visible everywhere in the program friendly(default) Visible everywhere in the current package. protected Visible everywhere in the current package and sub classes in other packages private Visible only in the class in which it is defined private protected Visible in its own class and all the sub class of all the packages Accessibility of member using various Access / Visibility modifiers: