32. Access modifier By Mr.
Vishnu
Access modifier in java
Access modifiers:
Access modifiers are keywords used for defining accessibility of classes, methods and
data members.
Types of access modifier.
1. Private.
2. Default
3. Protected
4. Public
Private:
Data members, methods and constructors that are declared with private access modifier
can be accessed into that class only.
Example:
PrivateAccessModifier.java
packagecom.sst;
/*This program is used to show that private members
of a class can be accessed in that class only*/
class Student {
// private members of the class
private int rollNo = 5;
private void showRollNo() {
// rollNo which a private data member is
// accessible in that class.
System.out.println("RollNo = " + rollNo);
}
}
public class PrivateAccessModifier {
public static void main(String args[]) {
1
Sri Sureka Technologies, NearAndraBank,Opposite Geethanjali High School, Near S.R.Nagar
UmeshChandraStachu, S.R.Nagar, Hyderabad-500038, Ph: 040-66616677, Mobile: +91-9885602332.
32. Access modifier By Mr. Vishnu
// creating Student class object
Student obj = new Student();
// compile time error because private members
// of a class can be accessed in that class only.
System.out.println(obj.rollNo);
obj.showRollNo();
}
Output:
Exception in thread "main" java.lang.Error:
Unresolved compilation problems:
The field Student.rollNo is not visible
The method showRollNo() from the type Student is not visible
at com.javawithease.business.PrivateAccessModifier.main
(PrivateAccessModifier.java:24)
Note: A class can have a private constructor but we cannot create an instance of
that class from outside the class.
PrivateConstructor.java
packagecom.sst;
/*This program is used to show that we cannot create an instance
of that class from outside the class if constructor is private.
*/
class Student {
// private constructor of the class
private Student() {
2
Sri Sureka Technologies, NearAndraBank,Opposite Geethanjali High School, Near S.R.Nagar
UmeshChandraStachu, S.R.Nagar, Hyderabad-500038, Ph: 040-66616677, Mobile: +91-9885602332.
32. Access modifier By Mr. Vishnu
public void show() {
System.out.println("Hello javawithease.com");
}
}
public class PrivateAccessModifier {
public static void main(String args[]) {
// compile time error in creating Student class object
// because of private constructor.
Student obj = new Student();
}
Output:
Exception in thread "main" java.lang.Error:
Unresolved compilation problem:
The constructor Student() is not visible
at com.javawithease.business.PrivateConstructor.main
(PrivateConstructor.java:22)
Note: Classes and interfaces can’t be private except nested classes.
Default:
Classes, data members, methods and constructors that are not declared with any
access modifier are treated as default. They can be accessed into all classes within the
same package only.
Example 1:
DefaultAccessModifier1.java
packagecom.sst;
3
Sri Sureka Technologies, NearAndraBank,Opposite Geethanjali High School, Near S.R.Nagar
UmeshChandraStachu, S.R.Nagar, Hyderabad-500038, Ph: 040-66616677, Mobile: +91-9885602332.
32. Access modifier By Mr. Vishnu
/*This program is used to show that members of the class
of a class can be accessed in the package level only.
*/
class Student {
//private members of the class
int rollNo = 5;
void showRollNo(){
System.out.println("RollNo = " + rollNo);
}
}
public class DefaultAccessModifier1 {
public static void main(String args[]) {
// creating Student class object
Student obj = new Student();
// No compile time error because members of the class
// of a class can be accessed in that package but can't be
// accessed outside the package.
System.out.println(obj.rollNo);
obj.showRollNo();
}
}
Output:
RollNo = 5
Example 2:
Display.java
packagecom.sst;
//This class is used to print a line.
public class Display {
void display(){
System.out.println("Hello vishnu.com");
4
Sri Sureka Technologies, NearAndraBank,Opposite Geethanjali High School, Near S.R.Nagar
UmeshChandraStachu, S.R.Nagar, Hyderabad-500038, Ph: 040-66616677, Mobile: +91-9885602332.
32. Access modifier By Mr. Vishnu
Test.java
packagecom.sst;
/*This program is used to show that default members of the class
are not accessible from outside the package.*/
public class Test {
public static void main(String[] args) {
//compile time error because default members of the class
//are not accessible from outside the package.
Display obj = new Display();
obj.display();
Output:
Hello vishnu.com
Protected:
Data members, methods and constructors that are declared with protected access
modifier can be accessed into all classes within the same package and only in
subclasses outside the package.
Example 1:
Display.java
packagecom.sst;
//This class is used to print a line.
public class Display {
void display(){
5
Sri Sureka Technologies, NearAndraBank,Opposite Geethanjali High School, Near S.R.Nagar
UmeshChandraStachu, S.R.Nagar, Hyderabad-500038, Ph: 040-66616677, Mobile: +91-9885602332.
32. Access modifier By Mr. Vishnu
System.out.println("Hello vishnu.com");
}
Test.java
package com.sst;
/*This program is used to show that protected members of the
class are accessible from outside the package in subclasses.
*/
public class Test extends Display {
public static void main(String[] args) {
Test obj = new Test();
// No compile time error because protected members
// of the class are accessible from outside the
// package in subclasses.
obj.display();
}
Output:
Hello vishnu.com
Example 2:
Display.java
packagecom.sst;
//This class is used to print a line.
public class Display {
void display(){
System.out.println("Hello vishnu.com");
}
6
Sri Sureka Technologies, NearAndraBank,Opposite Geethanjali High School, Near S.R.Nagar
UmeshChandraStachu, S.R.Nagar, Hyderabad-500038, Ph: 040-66616677, Mobile: +91-9885602332.
32. Access modifier By Mr. Vishnu
Test.java
package com.sst;
/*This program is used to show that protected members of the class
are not accessible from outside the package in non-subclasses.*/
public class Test{
public static void main(String args[]) {
Display obj = new Display();
// compile time error because protected members
// of the class are not accessible from outside
// the package in non-subclasses.
obj.display();
}
Output:
Hello vishnu.com
Note: Classes and interfaces can’t be protected except nested classes.
Public:
Classes, data members, methods and constructors that are declared with public access
modifier can be accessed everywhere.
Example:
Display.java
packagecom.sst;
//This class is used to print a line.
public class Display {
7
Sri Sureka Technologies, NearAndraBank,Opposite Geethanjali High School, Near S.R.Nagar
UmeshChandraStachu, S.R.Nagar, Hyderabad-500038, Ph: 040-66616677, Mobile: +91-9885602332.
32. Access modifier By Mr. Vishnu
void display(){
System.out.println("Hello vishnu.com");
}
Test.java
package com.sst;
/*This program is used to show that public members of the class
are accessible everywhere.*/
public class Test {
public static void main(String args[]) {
Display obj = new Display();
// public members of the class accessible everywhere.
obj.display();
}
Output:
Hello vishnu.com
8
Sri Sureka Technologies, NearAndraBank,Opposite Geethanjali High School, Near S.R.Nagar
UmeshChandraStachu, S.R.Nagar, Hyderabad-500038, Ph: 040-66616677, Mobile: +91-9885602332.