Skip to main content

Access Modifiers Permissions level and their scope

 


Access modifiers in Java determine the visibility and accessibility of classes, methods, and fields within your code. Here's a brief overview of each access modifier:

  1. Private:

    • Scope: Limited to the same class.
    • Permission:
      • Accessible within the class where it is declared.
      • Not accessible outside the class, including subclasses.
    • Example:
    • private int privateField;

  2. Default (Package-Private):

    • Scope: Limited to the same package.
    • Permission:
      • Accessible within the class where it is declared.
      • Accessible within any class in the same package.
      • Not accessible outside the package, including subclasses in other packages.
    • Example:
    • int packagePrivateField;

  3. Protected:

    • Scope: Limited to the same package and subclasses (including those in different packages).
    • Permission:
      • Accessible within the class where it is declared.
      • Accessible within any class in the same package.
      • Accessible by subclasses, even if they are in different packages.
      • Not accessible outside the package (except by subclasses).
    • Example:
    • protected int protectedField;

  4. Public:

    • Scope: Accessible from anywhere.
    • Permission:
      • Accessible within the class where it is declared.
      • Accessible within any class, regardless of the package.
      • Accessible by subclasses, regardless of the package.
    • Example:
    • public int publicField;


These access modifiers help you control the visibility of your code, providing a mechanism for encapsulation and information hiding. By choosing the appropriate access level, you can design classes and APIs that expose only what is necessary and hide implementation details. This promotes modularity, maintainability, and helps prevent unintended misuse of your code.

Within class

Within Package

Outside Package by subclass

Outside Package

Yes

No

No

No

Yes

Yes

No

No

Yes

Yes

Yes

No

Yes

Yes

Yes

Yes

See You soon.

Comments

Popular posts from this blog

A Well Comprehensive Test plan for Manual Testing

  Scope Definition: Define the scope based on specifications and requirements provided. However, also include exploratory testing to uncover issues that might not be explicitly mentioned in the requirements. Consider factors like usability, accessibility, security, and performance to broaden the scope beyond just functional testing. Considering factors like usability, accessibility, and security in the scope definition is essential for ensuring a comprehensive testing approach that goes beyond functional requirements Test Case Design: Use a combination of traditional methods like boundary value analysis, equivalence partitioning, and decision tables for structured testing. Incorporate exploratory testing techniques to explore the application and identify unexpected behaviour or edge cases. Prioritize test cases based on risk assessment and criticality. can implement the shift-left approach in...

Essential of Creating Test plan in Testing

  Why Test Plan is Vital Document! Test Planning process and plan itself serve as vehicle for communicating with other members of the project team, tester, Dev, BA, Peer and other stakeholders. This communication allows the test plan to influence the project team and allows the project team to influence the test plan. We can accomplish this Communication through circulation of one or more test plan drafts and through review meeting. E.g.: Please tell me what the plan is for releasing the test items into the test lab for each cycle of test execution E.g.: Please let me know which version of the test tool   will be used for the regression tests of the previous increment. A test plan Document becomes the record of previous discussions and agreements. Test Plan helps us to Manage the change if you are in Agile , have to this as Handy. Ways to write a Test Plan Document. Analyze the Product: Understand the product's objectives, target users, specific...

How test steps and Expected result and actual result should get differ each other and be unique?

  Test steps are action-oriented and describe the registration process, the expected result outlines the desired outcomes, and the actual result provides observations on what actually occurred during the execution of the test case.   Each section serves a unique purpose and together forms a comprehensive test case.  Any discrepancies between the expected and actual results can be easily identified and addressed . Test steps, expected results, and actual results in test cases should be distinct and unique to provide clarity, avoid ambiguity, and facilitate effective communication between testers and other stakeholders. Here are some guidelines on how to make them differ and unique: Test Steps: 1. Sequential and Clear: • Test steps should be listed in a sequential and clear manner.  • Each step should represent a specific action that the tester needs to perform. 2. Action-Oriented: • Use action-oriented language in test steps.  • Clearly state what actions ...