Java Program to Show Different Access Levels
Last Updated :
21 Apr, 2021
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
// java Program to illustrate One Single Class for Scope of
// Access modifiers
// Importing input output classes
import java.io.*;
// Main class
class GFG {
// Method 1
public static void method1()
{
// Print statement for method with public type
System.out.println(
"This method uses Public access modifier");
}
// Method 2
private static void method2()
{
// Print statement for method with private type
System.out.println(
"This method uses Private access modifier");
}
// Method 3
protected static void method3()
{
// Print statement for method with protected type
System.out.println(
"This method uses Protected access modifier");
}
// Method 4
static void method4()
{
// Print statement for static default type method
System.out.println(
"This method uses Default access modifier");
}
// Method 5
// Main driver method
public static void main(String[] args)
{
// Display message only
System.out.println(
"Various access modifiers being used in the same class");
// Calling all the above methods in main() method
// body
method1();
method2();
method3();
method4();
}
}
OutputVarious 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
// Java Program to illustrate Use of Two Different Classes
// in the Same Package to Show the Scope of Access modifiers
// Importing input output classes
import java.io.*;
// Main class
// Helper class
class Helper {
// Method 1
public static void method1()
{
// Print statement whenever this method is called
System.out.println(
"This method uses Public access modifier");
}
// Method 2
// Would not be accessed if set to "private"
public static void method2()
{
// Print statement whenever this method is called
System.out.println(
"This method uses Private access modifier");
}
// Method 3
protected static void method3()
{
// Print statement whenever this method is called
System.out.println(
"This method uses Protected access modifier");
}
// Method 4
static void method4()
{
// Print statement whenever this method is called
System.out.println(
"This method uses Default access modifier");
}
}
// Class 2
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Display message only
System.out.println(
"Various access modifiers being used in the same class");
// Calling all the above methods in main() method
// body
Helper.method1();
Helper.method2();
Helper.method3();
Helper.method4();
}
}
OutputVarious 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
// Java Program to access Private Members of a Class
// using Getter and Setter methods
// Class 1
// Helper class for AccessModifier
class Helper {
// Member variables of this class
// private fields
private int age;
private String name;
// Method 1
// Setter method used to set age, a private field
public void setAge(int age) { this.age = age; }
// Method 2
// Setter method used to set name, a private field
public void setName(String name) { this.name = name; }
// Method 3
// Getter method used to get age, a private field.
public int getAge() { return this.age; }
// Method 4
// Getter method used to get name, a private field
public String getName() { return this.name; }
}
// Class 2
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
Helper ob = new Helper();
// Set value of private fields
ob.setAge(888);
// Set value of private fields
ob.setName("Geeksfor Geeks ");
// Print and display the age using
// get value of private fields
System.out.println("Age: " + ob.getAge());
// Print and display the name using
// get value of private fields
System.out.println("Name: " + ob.getName());
}
}
OutputAge: 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.
Similar Reads
Java Directories Programs: Basic to Advanced Directories are an important part of the file system in Java. They allow you to organize your files into logical groups, and they can also be used to control access to files. In this article, we will discuss some of the Java programs that you can use to work with directories. We will cover how to cr
3 min read
Java Program to Traverse in a Directory A directory is an organizational file system structure that contains Files and Directorates. Even an attacker can try to traverse or access a folder which we name as 'File Traversal Attack or Path Traversal Attack a different directory. In short here the directory is traversed which is outside the h
3 min read
Java Program to Display all the Directories in a Directory The directory is the organizing structure of the computer file system which is not only responsible for storing files but also to locate them on the memory. File organization structure is a hierarchical structure involving parent and child relationships just like what is there in tree data structure
3 min read
Java Program to Show the Nesting of Methods In java, the methods and variables which we create in a class can only be called by using the object of that class or, in case of static methods, we can directly call it by using the name of the class. The methods and variables can be called with the help of the dot operator. But there is a special
5 min read
Java Threading Programs - Basic to Advanced Java threading is the concept of using multiple threads to execute different tasks in a Java program. A thread is a lightweight sub-process that runs within a process and shares the same memory space and resources. Threads can improve the performance and responsiveness of a program by allowing paral
3 min read
Java Exercises - Basic to Advanced Java Practice Programs with Solutions Looking for Java exercises to test your Java skills, then explore our topic-wise Java practice exercises? Here you will get 25 plus practice problems that help to upscale your Java skills. As we know Java is one of the most popular languages because of its robust and secure nature. But, programmers
7 min read
Menu-Driven program to implement Travel Agency Prerequisites: Classes and Objects in Java, Switch Case statement in Java Problem Statement: Write a program to build a simple application for the bus travelling service using JAVA 8, MYSQL Database and JDBC which can perform the following operations: Book ticket for users/passengers on given routes
15+ min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read
Java Object Oriented Programming - Exercises Looking for Java OOP exercises to test and improve your object-oriented programming skills? Explore our topic-wise Java OOP practice exercises, featuring over 25 practice problems designed to help you master key OOP concepts such as encapsulation, inheritance, polymorphism, and abstraction. Java is
15+ min read
How to Display all Threads Status in Java? Threads are light-weight processes within a process.. Multithreading in java is a feature that allows concurrent execution of two or more parts of a program to maximize the utilization of CPU. here the approach to retrieve the state of the thread is via getState() method of the Thread class. A java
2 min read