# Access Control

## What is Access Control?

In Java, we can specify the **level of access** certain variables and methods have. With this power, we can show or hide these variables to other classes and references on demand!

There are **4** modifier levels that get progressively more open:

* **Private:** Only this class can see it.
* **Package Protected (the default level):** All classes in the **same package** can see it.
* **Protected: Subclasses** (that inherit from the parent) can also see it.
* **Public:** All classes in the program can see it.

![A chart comparing the different access modifiers. The black bar is the default ("package protected").](/files/-M6pdSPdMemdAODmgKfS)

## Why do we need access control?

Access control works really well with other OOP concepts to help structure programs better and make them easier to understand. Here are some of the major benefits:

* Access control is **self documenting.** Usually, there's a reason for making certain variables private and others public, and no more needs to be said for that to be understood.
* **It's safe to change private methods without worrying about breaking things.** If a method is private, we know that the only references are within the same class, so we can edit them however we want without making other classes error as well.
* **Private/protected variables don't need to be understood by users.** If someone needs to use your program, they don't need to learn how to use any private methods since those will be hidden to them.

## Practice

Let's see how access control can be used to hide variables in different situations:

```java
package P;
public class A {
    int def; // Variable with default access
    protected int prot; // Variable with protected access
    private int priv; // Variable with private access
    
    static class NestedA { ... }
}

public class B extends A { ... }

===================
package Q;
public class C extends P.A { ... }

```

{% tabs %}
{% tab title="Question 1" %}
Which variables can be accessed in B?
{% endtab %}

{% tab title="Answer" %}
`def` and `prot` since B is in the same package as A.
{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="Question 2" %}
Which variables can be accessed in C?
{% endtab %}

{% tab title="Answer" %}
`prot` only, since C is in a different package but extends A.
{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="Question 3" %}
Which variables can be accessed in NestedA?
{% endtab %}

{% tab title="Answer" %}
None of them, because NestedA is static and cannot reference any non-static variables.
{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://cs61b.bencuan.me/oop/access-control.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
