SlideShare a Scribd company logo
1 of 19
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

Access Modifier.pptx
Access Modifier.pptxAccess Modifier.pptx
Access Modifier.pptxMargaret Mary
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Vineeta Garg
 
Access specifiers(modifiers) in java
Access specifiers(modifiers) in javaAccess specifiers(modifiers) in java
Access specifiers(modifiers) in javaHrithikShinde
 
Abstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaAbstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaMOHIT AGARWAL
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented ProgrammingIqra khalil
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator OverloadingNilesh Dalvi
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javaRahulAnanda1
 
Java package
Java packageJava package
Java packageCS_GDRCST
 
[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
 
Inheritance and Polymorphism Java
Inheritance and Polymorphism JavaInheritance and Polymorphism Java
Inheritance and Polymorphism JavaM. Raihan
 
itft-Inheritance in java
itft-Inheritance in javaitft-Inheritance in java
itft-Inheritance in javaAtul Sehdev
 

What's hot (20)

Access Modifier.pptx
Access Modifier.pptxAccess Modifier.pptx
Access Modifier.pptx
 
Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Interface
InterfaceInterface
Interface
 
Access specifiers(modifiers) in java
Access specifiers(modifiers) in javaAccess specifiers(modifiers) in java
Access specifiers(modifiers) in java
 
Chapter2 Encapsulation (Java)
Chapter2 Encapsulation (Java)Chapter2 Encapsulation (Java)
Chapter2 Encapsulation (Java)
 
inheritance
inheritanceinheritance
inheritance
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
Abstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaAbstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core Java
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Friend function in c++
Friend function in c++ Friend function in c++
Friend function in c++
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Abstract class
Abstract classAbstract class
Abstract class
 
Java package
Java packageJava package
Java package
 
[OOP - Lec 08] Encapsulation (Information Hiding)
[OOP - Lec 08] Encapsulation (Information Hiding)[OOP - Lec 08] Encapsulation (Information Hiding)
[OOP - Lec 08] Encapsulation (Information Hiding)
 
Inheritance and Polymorphism Java
Inheritance and Polymorphism JavaInheritance and Polymorphism Java
Inheritance and Polymorphism Java
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
itft-Inheritance in java
itft-Inheritance in javaitft-Inheritance in java
itft-Inheritance in java
 

Similar to Access modifiers in java

Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in javaAshwin Thadani
 
Visibility Modifiers for Access Control.pptx
Visibility Modifiers for Access Control.pptxVisibility Modifiers for Access Control.pptx
Visibility Modifiers for Access Control.pptxnaazminshaikh1727
 
Lecture 9 access modifiers and packages
Lecture   9 access modifiers and packagesLecture   9 access modifiers and packages
Lecture 9 access modifiers and packagesmanish kumar
 
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 OOPsAdrizaBera
 
Access Modifiers .pptx
Access Modifiers .pptxAccess Modifiers .pptx
Access Modifiers .pptxMDRakibKhan3
 
Access controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesAccess controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesVinay Kumar
 
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 ModifiersRatnaJava
 
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 Hitesh-Java
 
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 ModifiersPawanMM
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++RAJ KUMAR
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiersKhaled Adnan
 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxSodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxRudranilDas11
 

Similar to Access modifiers in java (20)

Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in java
 
Visibility Modifiers for Access Control.pptx
Visibility Modifiers for Access Control.pptxVisibility Modifiers for Access Control.pptx
Visibility Modifiers for Access Control.pptx
 
Lecture 9 access modifiers and packages
Lecture   9 access modifiers and packagesLecture   9 access modifiers and packages
Lecture 9 access modifiers and packages
 
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
 
00ps inheritace using c++
00ps inheritace using c++00ps inheritace using c++
00ps inheritace using c++
 
Access Modifiers .pptx
Access Modifiers .pptxAccess Modifiers .pptx
Access Modifiers .pptx
 
Access controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesAccess controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modes
 
10 access control
10   access control10   access control
10 access control
 
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
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Inheritance in c++theory
Inheritance in c++theoryInheritance in c++theory
Inheritance in c++theory
 
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
 
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
 
Packaes & interfaces
Packaes & interfacesPackaes & interfaces
Packaes & interfaces
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiers
 
Packages and interfaces
Packages and interfacesPackages and interfaces
Packages and interfaces
 
Unit 2 notes.pdf
Unit 2 notes.pdfUnit 2 notes.pdf
Unit 2 notes.pdf
 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxSodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
 

More from Madishetty Prathibha

More from Madishetty Prathibha (14)

Object Oriented programming - Introduction
Object Oriented programming - IntroductionObject Oriented programming - Introduction
Object Oriented programming - Introduction
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
Structure of java program diff c- cpp and java
Structure of java program  diff c- cpp and javaStructure of java program  diff c- cpp and java
Structure of java program diff c- cpp and java
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Types of datastructures
Types of datastructuresTypes of datastructures
Types of datastructures
 
Introduction to algorithms
Introduction to algorithmsIntroduction to algorithms
Introduction to algorithms
 
Java data types, variables and jvm
Java data types, variables and jvm Java data types, variables and jvm
Java data types, variables and jvm
 
Introduction to data structures (ss)
Introduction to data structures (ss)Introduction to data structures (ss)
Introduction to data structures (ss)
 
Java Tokens
Java  TokensJava  Tokens
Java Tokens
 
Oops concepts || Object Oriented Programming Concepts in Java
Oops concepts || Object Oriented Programming Concepts in JavaOops concepts || Object Oriented Programming Concepts in Java
Oops concepts || Object Oriented Programming Concepts in Java
 
Java features
Java  features Java  features
Java features
 
Introduction of java
Introduction  of javaIntroduction  of java
Introduction of java
 

Recently uploaded

Millenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxMillenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxJanEmmanBrigoli
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Presentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxPresentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxRosabel UA
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
EMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docxEMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docxElton John Embodo
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxruthvilladarez
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 

Recently uploaded (20)

Millenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxMillenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptx
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Presentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxPresentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptx
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
EMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docxEMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docx
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docx
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 

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: