- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Show Different Access Levels
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 visible only within a particular package.
Private - If we declare a function, it will visible only within a particular class only.
Protected- If we declare a function, it will visible only within a particular package or for all sub classes.
Public - If we declare a function, it will visible everywhere.
Example
class Study { public void method16() {...} private void method7() {...} }
Algorithm to show different access levels by using Java
Here is the possible algorithm to show different access levels by using Java −
Step 1 − Start.
Step 2 − Define a class which represents the particular object.
Step 3 − Define instance variables within a class.
Step 4 − Specify an access modifier. (In Java there are three access modifiers private, protected, and public.)
Step 5 − Use the private modifier for the variables.
Step 6 − Use protected for to access a class and sub class.
Step 7 − Use public modifiers to access anywhere.
Step 8 − To operate the variables declare the accessor and the mutator method.
Step 9 − Terminate.
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!"); } }
Here in this Java syntax we explain how to show different access levels by using Java environment.
Approaches to follow
Approach 1 − Using one single class to show the scope of Access modifiers.
Approach 2 − Using two different classes in the same package to show the scope of Access modifiers.
Approach 3 − Access Private data members of a class.
Approach 4 − Using all access modifires in different codes in a general manner.
Using one single class to show the scope of Access modifires
Here in this particular Java code, we use various types of access modifiers in a single class.
Example 1
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
Here in this particular Java code, we declare two different classes in the same package to show the scope of different access modifiers.
Example 2
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
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.
Example 3
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
Conclusion
In this article today, we have learned about the different types of access modifiers with some possible java codes by following the syntaxes and algorithm. Hope this article helped you to understand the modus operandi of the Java access levels functions mentioned here.