Home  >  Article  >  Java  >  FAQ about access modifiers for Java functions

FAQ about access modifiers for Java functions

WBOY
WBOYOriginal
2024-04-26 09:39:01610browse

Access permission modifiers are used to control member visibility. Java provides four modifiers: public (accessible to all classes), protected (accessible to classes and subclasses in this package), default (only classes in this package) accessible), private (accessible only within this class). According to inheritance rules, subclasses inherit the access rights of the parent class, but cannot expand them. Restricting access to a class or interface can be achieved through the private modifier. The difference between protected and default modifiers is that the former allows access by subclasses and classes within this package, while the latter only allows access by classes within this package.

Java 函数的访问权限修饰符之常见问题解答

FAQs about Java function access modifiers

Access modifiers are used to control classes, interfaces, and methods Visibility of other members. Java provides four access modifiers:

  • public: Available to all classes
  • protected: Available to this package Access to inner classes and subclasses
  • default (package): Accessible to classes within this package
  • private: Accessible only to this class

FAQ:

Q1: How to use access modifiers?

A: When defining class members, use the appropriate access modifier as a prefix, for example:

public class Person {
    private String name;
}

Q2: Access modifier What are the inheritance rules for symbols?

A: The subclass inherits the access rights of the parent class, but cannot expand it. For example:

class Parent {
    protected void doSomething() { }
}

class Child extends Parent {
    void doSomething() { } // 子类不能将保护方法声明为默认方法
}

Q3: How to restrict access to a class or interface?

A: Use the private access modifier to make it private so that it can be used only by yourself.

Q4: What is the difference between protected and default access modifiers?

A: protected access permission allows access to subclasses and classes within this package, while default access permission only allows access to this package Class access within the package.

Practical case:

Create a Employee class with name, salary and jobDescription Private fields and provide getter and setter methods:

public class Employee {
    private String name;
    private double salary;
    private String jobDescription;

    public String getName() {
        return name;
    }

    public double getSalary() {
        return salary;
    }

    public String getJobDescription() {
        return jobDescription;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public void setJobDescription(String jobDescription) {
        this.jobDescription = jobDescription;
    }
}

In this example, the private fields restrict direct access, while the getter and setter methods provide controlled indirect access.

The above is the detailed content of FAQ about access modifiers for Java functions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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