What is the purpose of access modifiers?

后端 未结 6 2132
-上瘾入骨i
-上瘾入骨i 2020-12-19 03:04

I know this applies to many languages, and not just Java, but that is the language I\'m most familiar with.

I understand what the modifiers do, and how to use them.

6条回答
  •  天涯浪人
    2020-12-19 04:05

    Explanations

    A private member is only accessible within the same class as it is declared. A member with no access modifier is only accessible within classes in the same package.

    or

    If a variable is set to protected inside a Class, it will be accessible from its sub classes defined in the same classes or different package only via Inheritance.

    A protected member is accessible within all classes in the same package and within subclasses in other packages.

    A public member is accessible to all classes (unless it resides in a module that does not export the package it is declared in Here's a better version of the table. (Future proof with a column for modules.)

提交回复
热议问题