Home > Java > javaTutorial > body text

Things to note when using access modifiers for Java functions

WBOY
Release: 2024-04-25 17:09:01
Original
589 people have browsed it

Java function access permission modifiers include: public, protected, default and private. The following precautions need to be followed: Nested classes can only access private members of external classes; functions in subclasses inherit the access permissions of the parent class, but cannot reduce them; under polymorphism, when subclasses override parent class functions, access permissions cannot be more restrictive. The ;default modifier makes the function visible only within the same package.

Java 函数的访问权限修饰符之使用时的注意事项

Access modifiers for Java functions: Precautions when using them

Preface

Access modifiers are used to control the visibility of Java functions to other classes or packages, which is crucial to ensuring the encapsulation and security of the code. This article will introduce the precautions for using function access permission modifiers in Java and illustrate them through practical cases.

Access permission modifiers

Commonly used function access permission modifiers in Java include:

  • public: Accessible anywhere
  • protected: Accessible within the same package or its subclasses
  • default (package-private): Only accessible Access
  • private within the same package: Can only be accessed within the class in which they are defined

Notes

When using the access permission modifier, you need to follow the following precautions:

  • Nested classes: Functions defined in nested classes can only access the private properties of their outer classes member.
  • Subclass: The functions in the subclass inherit the access rights of the parent class, but cannot reduce the access rights of the parent class.
  • Polymorphism: Subclasses can override functions of the parent class, but the access permissions of the overridden functions cannot be more restrictive than the access permissions of the parent class functions.
  • Package visibility: The default modifier can also be called package visibility, which means that the function is only visible in classes in the same package.

Practical case

Demonstrates a code example containing two classes to illustrate the use of access permission modifiers:

// 外部类
public class OuterClass {
    private int privateField;  // 私有字段
    protected int protectedField;  // 受保护字段
    int defaultField;  // 默认字段
    public int publicField;  // 公共字段

    // 私有方法
    private void privateMethod() {
        System.out.println("私有方法");
    }
    // 受保护方法
    protected void protectedMethod() {
        System.out.println("受保护方法");
    }
    // 默认方法
    void defaultMethod() {
        System.out.println("默认方法");
    }
    // 公共方法
    public void publicMethod() {
        System.out.println("公共方法");
    }
}

// 内部类
class InnerClass {
    public static void main(String[] args) {
        OuterClass outer = new OuterClass();

        // 访问内部类中的公共字段
        System.out.println(outer.publicField);

        // 访问外部类中的默认字段(因为内部类和外部类在同一包中)
        System.out.println(outer.defaultField);

        // 无法访问外部类中的私有字段
        // System.out.println(outer.privateField);

        // 无法访问外部类中的受保护字段(因为内部类不是外部类的子类)
        // System.out.println(outer.protectedField);

        // 无法调用外部类中的私有方法
        // outer.privateMethod();

        // 可以调用外部类中的受保护方法
        outer.protectedMethod();

        // 可以调用外部类中的默认方法
        outer.defaultMethod();

        // 可以调用外部类中的公共方法
        outer.publicMethod();
    }
}
Copy after login

In this In the example:

privateField
    in
  • OuterClass can only be accessed in OuterClass.
  • protectedField
  • in OuterClass can be accessed in OuterClass and its subclasses. The
  • defaultField
  • in OuterClass can be accessed from any class in the same package.
  • publicField
  • in OuterClass can be accessed from anywhere.
  • InnerClass Can access public, protected, and default members in OuterClass, but not private members.

The above is the detailed content of Things to note when using access modifiers for Java functions. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template