Object-Oriented Programming (Advanced) 4: Talking about 4 Permission Modifications in Encapsulation

Table of contents

Permission modifiers: public, protected, default, private

1. The external class must be public if it is to be used across packages, otherwise it is limited to this package

(1) If the permission modifier of the external class is defaulted, there is no problem in using this package

(2) If the permission modifier of the external class is default, there is a problem with cross-package use

2. Member permission modifier problem

(1) Used in this package: the permission modifiers of members can be public, protected, default

(2) Use across packages: strict requirements

(3) When using across packages, if the permission modifier of the class is default, the member permission modifier > the permission modifier of the class is also meaningless


Permission modifiers: public, protected, default, private

Modifier This class This bag Other buns other package non-subclass
private × × ×
default √ (This package is visible to both subcategories and non-subcategories) × ×
protected √ (This package is visible to both subcategories and non-subcategories) √ (Other packages are only visible in subclasses) ×
public

External class: public and default

Member variables, member methods, etc.: public, protected, default, private

1. The external class must be public if it is to be used across packages, otherwise it is limited to this package

(1) If the permission modifier of the external class is defaulted, there is no problem in using this package

(2) If the permission modifier of the external class is default, there is a problem with cross-package use

2. Member permission modifier problem

(1) Used in this package: the permission modifiers of members can be public, protected, default

(2) Use across packages: strict requirements

(3) When using across packages, if the permission modifier of the class is default, the member permission modifier > the permission modifier of the class is also meaningless

Guess you like

Origin blog.csdn.net/swx595182208/article/details/130014925