
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Show Different Access Levels in Java
In this article, we will explore the use of access modifiers in Java by demonstrating their functionality in different scenarios.
Access Modifiers
Access modifiers are used to set the feature of visibility of some particular classes, interfaces, variables, methods, constructors, data members, and the setter methods in Java programming language.
In a Java environment, we have different types of access modifiers.
-
Default - If we declare a function, it will be visible only within a particular package.
-
Private - If we declare a function, it will be visible only within a particular class.
-
Protected- If we declare a function, it will be visible only within a particular package or for all sub-classes.
-
Public - If we declare a function, it will be visible everywhere.
Syntax to show different access levels by using Java
Java program to define default modifier:
package a1; class Tutorialspoint{ void display(){ System.out.println("Welcome To Tutorialspoint!"); } }
Java program to define private modifier:
package a1; class A07{ private void display(){ System.out.println("Welcome To Tutorialspoint!"); } } class B07{ public static void main(String args[]){ A obj = new A(); obj.display(); } }
Java program to define protected modifier:
package a1;
public class A07{
protected void display(){
System.out.println("Welcome To Tutorialspoint!");
}
}
Java program to define public modifier:
package a1; public class A{ public void display(){ System.out.println("Welcome To Tutorialspoint!"); } }
Different methods
The following are the different methods to show different access levels ?
-
Using one single class to show the scope of Access modifiers.
-
Using two different classes in the same package to show the scope of Access modifiers.
-
Access Private data members of a class.
-
Using all access modifiers in different codes in a general manner.
Using one single class to show the scope of Access modifiers
The following are the steps for using one single class to show the scope of Access modifiers ?
-
Step 1. Declare the class with methods using different access modifiers: The class tutorialspoint has methods with various access modifiers: public, private, protected, and default (no modifier).
public class tutorialspoint {
public static void method07() { ... }
private static void method16() { ... }
protected static void method10() { ... }
static void method9701() { ... }
}
-
Step 2. Main method for testing the access of methods: Inside the main() method, all the methods are invoked to test their accessibility within the same class.
public static void main(String[] args) {
System.out.println("Various access modifiers being used in the same class");
method07();
method16();
method10();
method9701();
}
Example 1
Here in this particular Java code, we use various types of access modifiers in a single class.
import java.io.*; public class tutorialspoint { public static void method07(){ System.out.println("This method uses Public access modifier - method07"); } private static void method16(){ System.out.println("This method uses Private access modifier-method16"); } protected static void method10(){ System.out.println("This method uses Protected access modifier-method10"); } static void method9701(){ System.out.println("This method uses Default access modifier-method10"); } public static void main(String[] args){ System.out.println("Various access modifiers being used in the same class"); method07(); method16(); method10(); method9701(); } }
Output
Various access modifiers being used in the same class This method uses Public access modifier - method07 This method uses Private access modifier-method16 This method uses Protected access modifier-method10 This method uses Default access modifier-method10
Using two different classes in the same package to show the scope of Access modifiers
The following are the steps for using two different classes in the same package to show the scope of Access modifiers ?
-
Step 1. Declare a helper class with various access modifier methods: The Helper class contains methods that demonstrate the use of public, protected and default access modifiers.
class Helper {
public static void method1() { ... }
public static void method2() { ... }
protected static void method3() { ... }
static void method4() { ... }
}
-
Step 2. Create a main class to invoke methods from the helper class: The class TP accesses the methods from Helper to demonstrate the scope of different access modifiers in the same package.
public class TP {
public static void main(String[] args) {
Helper.method1();
Helper.method2();
Helper.method3();
Helper.method4();
}
}
Example 2
Here in this particular Java code, we declare two different classes in the same package to show the scope of different access modifiers.
import java.io.*; class Helper { public static void method1(){ System.out.println("I Want To Travel Varanasi"); } public static void method2(){ System.out.println("It Is In UP, India"); } protected static void method3(){ System.out.println("Doon Express Is The Best Option"); } static void method4(){ System.out.println("__________________"); } } public class TP { 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 I Want To Travel Varanasi It Is In UP, India Doon Express Is The Best Option
Access Private data members of a class
The following are the steps for accessing the private data of members of a class ?
-
Step 1. Declare a class with private data members and getter/setter methods: The Helper class has two private fields, age and name. It also includes public getter and setter methods to access and modify these fields.
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; }
}
-
Step 2. Main method to set and retrieve the private data using getter and setter methods: In the main() method, an object of the Helper class is created, and the getter/setter methods are used to set and retrieve the private data.
public class Tutorialspoint {
public static void main(String[] args){
Helper ob = new Helper();
ob.setAge(2001);
ob.setName("We Are The Tutors Of Tutorialspoint");
System.out.println("Age: " + ob.getAge());
System.out.println("Name: " + ob.getName());
}
}
Example 3
Here in this Java build code we explain the getter and setter method. By this practice, we can fetch and set the value of various parameters in a Java Virtual Machine.
import java.io.*; 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 Tutorialspoint { public static void main(String[] args){ Helper ob = new Helper(); ob.setAge(2001); ob.setName("We Are The Tutors Of Tutorialspoint"); System.out.println("Age: " + ob.getAge()); System.out.println("Name: " + ob.getName()); } }
Output
Age: 2001 Name: We Are The Tutors Of Tutorialspoint