• IB D3.3Define the terms: private, protected, public, extends, static


     Access modifiers

    • Access level modifiers determine whether other classes can use a particular field or invoke a particular method.

    访问级别修饰符决定其他类是否可以使用特定的字段或调用特定的方法。

    • A class may be declared with the modifier public, in which case that class is visible to all classes everywhere.

    • If a class has no modifier (i.e. the default position) it is visible only within its own package. • There are three Java access modifiers:

    – public – protected – private

    Definition: public

    • A class, method, field or constructor that is declared public can be accessed from any other class.

    • Therefore, fields, methods, blocks declared inside a public class can be accessed from any class belonging to the Java Universe.

    • Because of class inheritance, all public methods and variables of a class are inherited by its subclasses.

    Definition: private

    • Methods, variables, and constructors that are declared private can only be accessed within the declared class itself.

    • private is the most restrictive access level.

    • Classes cannot be private, but methods and variables can.

    • Variables that are declared private can be accessed outside the class, if public getter methods are present in the class.

    • Using the private modifier is the main way that an object encapsulates itself and hides data from the outside world.

    Definition: protected

    • Variables, methods, and constructors, which are declared protected in a superclass can be accessed only by the subclasses in any class within the package of the protected members' class.

    • protected cannot be applied to classes.

    • protected access gives the subclass a chance to use the helper method or variable, while preventing a nonrelated class from trying to use it.

  • 相关阅读:
    挂载硬盘相关操作-linux004
    圆的反演 hdu 6158
    表单嵌套表格,实现表格行内表单的校验
    使用ChatGPT创建Makefile构建系统:使用Make运行Docker
    nginx配置ssl证书
    stm32cubemx安装(出现JDK配置错误,导致无法安装)
    eNsp使用技巧
    PTA-L2-004 这是二叉搜索树吗?
    gradle编译spring源码过程问题整理
    140.深度学习分布式计算框架-3
  • 原文地址:https://blog.csdn.net/weixin_38665509/article/details/126280710