Home Java javaTutorial Rules for using access modifiers of Java functions in different classes

Rules for using access modifiers of Java functions in different classes

Apr 25, 2024 pm 01:54 PM
java access Compile Error access permission

When using Java functions in different classes, follow the following access rights rules: Default access rights are limited to classes within the same package. Package visibility is the same as default access, but applies to all classes in the same module. Protected access is limited to subclasses and classes in the same package. Public access rights are visible in all classes.

Java 函数的访问权限修饰符之在不同类中使用的规则

The rules for using access permission modifiers in Java functions in different classes

The access permission modifiers in Java use Used to control the visibility scope of classes, methods, and fields. When using functions in different classes, you need to abide by the following rules:

1. Default access permissions (no modifiers)

  • Only within the same package Visible in class

2. Package visibility (default)

  • Same as default access, but applies to all classes in the same module

3. protected access

  • Visible only in subclasses of this class and classes in the same package

4. Public access rights

  • Visible in all classes

Practical case

Consider the following code:

// MySuperClass.java
public class MySuperClass {
    protected void protectedMethod() {
        // ...
    }

    public void publicMethod() {
        // ...
    }
}
Copy after login
// MySubClass.java
public class MySubClass extends MySuperClass {
    // 可访问父类的 protected 和 public 方法
    void accessProtected() {
        protectedMethod();
    }

    void accessPublic() {
        publicMethod();
    }
}
Copy after login
// MyOtherClass.java
public class MyOtherClass {
    // 无法访问父类的 protected 或 public 方法
    void accessProtected() {
        // 编译错误: protectedMethod() 具有 protected 访问权限,在此包外不可访问
    }

    void accessPublic() {
        // 编译错误: publicMethod() 具有 public 访问权限,但在不同的模块中不可访问
    }
}
Copy after login

In this example, MyOtherClass cannot access protectedMethod() and publicMethod( in MySuperClass ) because they are not in the same package or are not subclasses of MySuperClass.

Notes

  • Inner classes can access all members of their outer classes, regardless of access modifiers.
  • The compiler will check the access permissions, and if the rules are violated, a compilation error will be generated.

The above is the detailed content of Rules for using access modifiers of Java functions in different classes. 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)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
PHP's Impact: Web Development and Beyond PHP's Impact: Web Development and Beyond Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP vs. Python: Use Cases and Applications PHP vs. Python: Use Cases and Applications Apr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

Using Dicr/Yii2-Google to integrate Google API in YII2 Using Dicr/Yii2-Google to integrate Google API in YII2 Apr 18, 2025 am 11:54 AM

VprocesserazrabotkiveB-enclosed, Мнепришлостольностьсясзадачейтерациигооглапидляпапакробоглесхетсigootrive. LEAVALLYSUMBALLANCEFRIABLANCEFAUMDOPTOMATIFICATION, ČtookazaLovnetakProsto, Kakaožidal.Posenesko

How to solve nginx current limit How to solve nginx current limit Apr 14, 2025 pm 12:06 PM

The Nginx current limit problem can be solved by: use ngx_http_limit_req_module to limit the number of requests; use ngx_http_limit_conn_module to limit the number of connections; use third-party modules (ngx_http_limit_connections_module, ngx_http_limit_rate_module, ngx_http_access_module) to implement more current limit policies; use cloud services (Cloudflare, Google Cloud Rate Limiting, AWS WAF) to DD

Title: Use Composer to solve the problem of unified representation of complex data types Title: Use Composer to solve the problem of unified representation of complex data types Apr 18, 2025 am 08:33 AM

Summary Description: When dealing with complex data types, you often encounter problems of how to uniformly represent and operate. This problem can be easily solved with Composer using the phrity/o library. It provides encapsulation classes and traits for various data types, making data processing more consistent and efficient.

How to build miniio in centos How to build miniio in centos Apr 14, 2025 pm 04:42 PM

The steps to build a MinIO server on CentOS are as follows: 1. Install MinIO First, you need to download and install MinIO. You can download the latest version of MinIO binary from MinIO's official website. wgethttps://dl.min.io/server/minio/release/linux-amd64/miniochmod xminiosudomvminio/usr/local/bin/2. Create Mi

Can vs code compile c Can vs code compile c Apr 15, 2025 pm 08:12 PM

VS Code can be used to compile C, and it has high efficiency and good debugging experience, thanks to its rich expansion ecosystem. The specific steps include: installing the C compiler (such as GCC, Clang). Install the C/C extension. Compile C code using a built-in terminal or build system such as CMake. Use the debugger to debug code. Improve development efficiency with code snippets and custom tasks.

Solve memory management problems in Magento project: application of zend-memory library Solve memory management problems in Magento project: application of zend-memory library Apr 17, 2025 pm 11:03 PM

I'm having a tough memory management problem while working on a Magento project. As the project involves a large amount of data processing, memory consumption increases rapidly, resulting in system performance degradation and even crashes. After some research, I discovered the zend-memory library, which effectively solved my memory management problem.

See all articles