JAVA Programming
Lecture 14: Access Modifiers in JAVA
Department of Computer Science and Engineering
National Institute of Technology, Srinagar, Jammu and Kashmir
April 08, 2024
Access Modifiers in JAVA
• The access modifiers in Java specifies the accessibility or scope of a
field, method, constructor, or class.
• We can change the access level of fields, constructors, methods, and
class by applying the access modifier on it.
Access Modifiers in JAVA
There are four types of Java access modifiers:
1. Private
2. Default
3. Protected
4. Public
Private Access Modifiers in JAVA
Private Access Modifier: The access level of a private modifier is only within
the class. It cannot be accessed from outside the class.
Any property (variable, method) defined as private can be used and accessed
within the class only.
Private Access Modifiers in JAVA (Output?)
class AccessModifier_Private
{
private int age=10;
private void print()
{
System.out.println(age);
}
public static void main(String[] args)
{
AccessModifier_Private obj=new AccessModifier_Private();
obj.print();
}
}
Private Access Modifiers in JAVA
class AccessModifier_Private
{
private int age=10; //it is private and can be used within this Class only
private void print() //it is private and can be used within this Class only
{
System.out.println(age);
}
public static void main(String[] args)
{
AccessModifier_Private obj=new AccessModifier_Private();
obj.print(); //we are accessing the private print() inside this class only. So, it will work.
}
}
Private Access Modifiers (Output?)
class AccessModifier_Private
{
private int age=10;
private void print()
{
System.out.println(age);
}
}
public class AccessModifier_Private2
{
public static void main(String[] args)
{
AccessModifier_Private obj=new AccessModifier_Private();
obj.print();
}
}
Private Access Modifiers (Solution)
class AccessModifier_Private
{
private int age=10; //it is private and can be used within this Class only
private void print() //it is private and can be used within this Class only
{
System.out.println(age);
}
}
public class AccessModifier_Private2
{ public static void main(String[] args)
{
AccessModifier_Private obj=new AccessModifier_Private();
obj.print(); //we are accessing the private print() outside the class. So, error
}
}
Output?
class A{
private A(){
System.out.println(“Constructor ”);
}
void msg(){
System.out.println("Hello java");
}}
public class Simple{
public static void main(String args[]){
A obj=new A();
}
}
Private Constructor
• If the constructor is made Private, then we cannot create object of that Class from
outside the class.
class A{
private A(){ //private constructor
System.out.println(“Constructor ”);
}
void msg(){
System.out.println("Hello java");
}}
public class Simple{
public static void main(String args[]){
A obj=new A(); //Compile Time Error
}
}
Output if Class is made as Private?
Output if Class is made as Private?
• A class cannot be private or protected except nested class.
2. Default Access Modifiers in JAVA
Default Access Modifier:
• The access level of a default modifier is only within the package.
• The property with Default access modifier can be accessed by accessed by
other classes in same package.
• It cannot be accessed from outside the package.
• If you do not specify any access level, it will be the default.
• It provides more accessibility than private. But, it is more restrictive than
protected, and public.
Default Access Modifiers (Example 1)
Default Access Modifiers (Example 2)
Default Access Modifiers (Example 3) Output?
Solution: Default Access Modifiers (Example 3)?
3. Protected Access Modifiers in JAVA
Protected Access Modifier:
• The access level of a protected modifier is within the package and outside the
package through child class.
• The protected access modifier is accessible within package and outside the
package but through inheritance only.
• If you do not make the child class, it cannot be accessed from outside the
package.
• The protected access modifier can be applied on the data member, method
and constructor.
• We cannot apply Protected access modifier on a Class.
• It provides more accessibility than the default modifer.
Protected Access Modifiers (Example 1)
Protected Access Modifiers (Example 2)
4. Public Access Modifier
• The public access modifier is accessible everywhere.
• It has the widest scope among all other modifiers.
Access Modifiers in JAVA
outside
Access outside
within class within package package by
Modifier package
subclass only
Private Y N N N
Default Y Y N N
Protected Y Y Y N
Public Y Y Y Y