The access levels in a program can be introduced by the concept of Access Modifiers. Access Modifier in a java program is used to specify the scope of a field, method, constructor, or even a class. We can hence change the access level of fields, constructors, methods, and class by applying the access modifier to it. There are 4 types of access modifiers namely as follows:
Access Modifier |
Description |
Public |
The access level of a public modifier is everywhere. It can be accessed from within the class, outside the class, within the package, and even outside the package. |
Private |
The access level of a private modifier is only within the class, the modifier is mentioned in. It cannot be accessed from outside that class. |
Protected |
The access level of a protected modifier is within the package (it is mentioned in) and outside the package through child class. If you do not make the child class, it cannot be accessed from outside the package. |
Default |
The access level of a default modifier is only within the package. It cannot be accessed from outside the package. If you do not specify any access level, it will be set to default. |
Implementation: Access Modifier usages
Example 1: Using one single class to show the scope of Access modifiers
Java
import java.io.*;
class GFG {
public static void method1()
{
System.out.println(
"This method uses Public access modifier" );
}
private static void method2()
{
System.out.println(
"This method uses Private access modifier" );
}
protected static void method3()
{
System.out.println(
"This method uses Protected access modifier" );
}
static void method4()
{
System.out.println(
"This method uses Default access modifier" );
}
public static void main(String[] args)
{
System.out.println(
"Various access modifiers being used in the same class" );
method1();
method2();
method3();
method4();
}
}
|
Output
Various access modifiers being used in the same class
This method uses Public access modifier
This method uses Private access modifier
This method uses Protected access modifier
This method uses Default access modifier
Example 2: Using two different classes in the same package to show the scope of Access modifiers
Java
import java.io.*;
class Helper {
public static void method1()
{
System.out.println(
"This method uses Public access modifier" );
}
public static void method2()
{
System.out.println(
"This method uses Private access modifier" );
}
protected static void method3()
{
System.out.println(
"This method uses Protected access modifier" );
}
static void method4()
{
System.out.println(
"This method uses Default access modifier" );
}
}
class GFG {
public static void main(String[] args)
{
System.out.println(
"Various access modifiers being used in the same class" );
Helper.method1();
Helper.method2();
Helper.method3();
Helper.method4();
}
}
|
Output
Various access modifiers being used in the same class
This method uses Public access modifier
This method uses Private access modifier
This method uses Protected access modifier
This method uses Default access modifier
Note:
In method 2 if we set access modifier to “private” we get error as:
prog.java:25: error: method2() has private access in SecondClass
SecondClass.method2();// calling method2
(Reason: private members cannot be accessed outside the class)
Let us discuss the above missing scenario where we are accessing private members of a class we can use getter and setter methods to fetch and set the value of various parameters in Java.
Example 3: Access Private data members of a class
Java
class Helper {
private int age;
private String name;
public void setAge( int age) { this .age = age; }
public void setName(String name) { this .name = name; }
public int getAge() { return this .age; }
public String getName() { return this .name; }
}
public class GFG {
public static void main(String[] args)
{
Helper ob = new Helper();
ob.setAge( 888 );
ob.setName( "Geeksfor Geeks " );
System.out.println( "Age: " + ob.getAge());
System.out.println( "Name: " + ob.getName());
}
}
|
Output
Age: 888
Name: Geeksfor Geeks
Conclusion: So there are 4 access modifiers to specify the scope of methods, fields, classes or constructors, illustrated by Java program examples.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!