Java Program to Show Different Access Levels - GeeksforGeeks

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:
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 classesimport java.io.*;  // Main classclass 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();    }}
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
// Java Program to illustrate Use of Two Different Classes// in the Same Package to Show the Scope of Access modifiers  // Importing input output classesimport java.io.*;  // Main class// Helper classclass 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 classclass 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();    }}
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
// Java Program to access Private Members of a Class// using Getter and Setter methods  // Class 1// Helper class for AccessModifierclass 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 classpublic 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());    }}
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.
Java-Modifier
Picked
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Hashtable in Java
Constructors in Java
Different ways of Reading a text file in Java
Comparator Interface in Java with Examples
Java Math random() method with Examples
Convert a String to Character array in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
How to Iterate HashMap in Java?