Table of Contents
Example 3
Use Java to demonstrate algorithms for different access levels
Syntax for displaying different access levels using Java
Java program defines private modifiers:
Java program defines protected modifier:
Java program defines public modifier:
How to follow
Use a single class to display the scope of access modifiers
Output
Use two different classes in the same package to show the scope of access modifiers
Access private data members of a class
in conclusion
Home Java javaTutorial Java programs display different access levels

Java programs display different access levels

Aug 19, 2023 pm 10:09 PM
private public access level demo (java) protected

Java programs display 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 Java environment, we have different types of access modifiers.

  • Default - If we declare a function, it will only be visible in a specific package.

  • Private - If we declare a function, it will be visible only within a specific class.

  • Protected- If we declare a function, it will be visible only within a specific package or in all subclasses.

  • Public - If we declare a function, it will be visible everywhere.

The Chinese translation of

Example

is:

Example

class Study {
   public void method16() {...}

   private void method7() {...}
}
Copy after login

Use Java to demonstrate algorithms for different access levels

The following are possible algorithms to show different access levels using Java:

  • Step one - start.

  • Step 2 - Define a class that represents a specific object.

  • Step 3 - Define instance variables in the class.

  • Step 4 - Specify access modifiers. (There are three access modifiers in Java: private, protected and public.)

  • Step 5 - Use private modifier on variables.

  • Step 6 - Use the protected keyword to access a class and subclasses.

  • Step 7 - Access anywhere using the public modifier.

  • Step 8 - In order to manipulate variables, declare accessor and modifier methods.

  • Step 9 - Termination.

Syntax for displaying different access levels using Java

Java program defines default modifiers:

package a1;
class Tutorialspoint{
   void display(){
      System.out.println("Welcome To Tutorialspoint!");
   }
}
Copy after login

Java program defines private modifiers:

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();
   }
}
Copy after login

Java program defines protected modifier:

package a1;
public class A07{
   protected void display(){
       System.out.println("Welcome To Tutorialspoint!");
   }
}
Copy after login

Java program defines public modifier:

package a1;
public class A{
   public void display(){
       System.out.println("Welcome To Tutorialspoint!");
   }
}
Copy after login

In this Java syntax, we explain how to display different access levels by using the Java environment.

How to follow

  • Method 1 - Use a single class to display the scope of access modifiers.

  • Method 2−Use two different classes in the same package to show the scope of access modifiers.

  • Method 3 - Access private data members of a class.

  • Method 4 − Use all access modifiers in different codes in a general way.

Use a single class to display the scope of access modifiers

In this particular Java code, we are using various types of access modifiers in a class.

The Chinese translation of

Example 1

is:

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();
   }
}
Copy after login

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
Copy after login

Use two different classes in the same package to show the scope of access modifiers

In this particular Java code, we declared two different classes in the same package to demonstrate 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();
   }
}

Copy after login

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
Copy after login

Access private data members of a class

In this Java build code, we explain the getter and setter methods. Through this practice, we can get and set the values ​​​​of various parameters in the Java virtual machine.

The Chinese translation of

Example 3

is:

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());
   }
}
Copy after login

Output

Age: 2001
Name: We Are The Tutors Of Tutorialspoint
Copy after login

in conclusion

In this article, we learned about different types of access modifiers and some possible Java codes by following the syntax and algorithm. Hopefully this article helped you understand how the Java access level functions mentioned here operate.

The above is the detailed content of Java programs display different access levels. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

What is the difference between memory leaks in Java programs on ARM and x86 architecture CPUs? What is the difference between memory leaks in Java programs on ARM and x86 architecture CPUs? Apr 19, 2025 pm 11:18 PM

Analysis of memory leak phenomenon of Java programs on different architecture CPUs. This article will discuss a case where a Java program exhibits different memory behaviors on ARM and x86 architecture CPUs...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to correctly divide business logic and non-business logic in hierarchical architecture in back-end development? How to correctly divide business logic and non-business logic in hierarchical architecture in back-end development? Apr 19, 2025 pm 07:15 PM

Discussing the hierarchical architecture problem in back-end development. In back-end development, common hierarchical architectures include controller, service and dao...

How to convert names to numbers to implement sorting within groups? How to convert names to numbers to implement sorting within groups? Apr 19, 2025 pm 01:57 PM

How to convert names to numbers to implement sorting within groups? When sorting users in groups, it is often necessary to convert the user's name into numbers so that it can be different...

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

What is the reason why the browser does not respond after the WebSocket server returns 401? How to solve it? What is the reason why the browser does not respond after the WebSocket server returns 401? How to solve it? Apr 19, 2025 pm 02:21 PM

The browser's unresponsive method after the WebSocket server returns 401. When using Netty to develop a WebSocket server, you often encounter the need to verify the token. �...

See all articles