Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Classes and Objects  ›  Lesson

Access Specifiers and Encapsulation

Access Specifiers 14 minThe private, package-private, protected and public levels control which classes can use a member, and encapsulation keeps fields private behind methods that enforce the class's rules.

Access Specifiers and Encapsulation

A new developer at Brew Bar writes order.cups = -3; in the refund screen, and the day's takings go negative. Nothing stopped them, because the field was open to every class. Access specifiers let a class decide who may touch what, so its rules cannot be skipped.

The rule

Make fields private; open only the doors you mean to.

Java has four levels of access, from tightest to widest:

Keyword Who can use it
private only code inside the same class
(no keyword) any class in the same package
protected the same package, plus subclasses elsewhere
public every class, everywhere

A package is a named folder of related classes, declared on the first line of each file, such as package com.brewbar.orders;. Classes in other packages bring it in with import com.brewbar.orders.Order;. Packages both organise code and set the boundary for the no-keyword level, called package-private.

Encapsulation means hiding an object's fields and exposing only methods that keep its rules:

package com.brewbar.orders;

public class Order {
    private int cups = 1;

    public int getCups() {
        return cups;
    }

    public void addCups(int extra) {
        if (extra < 1) {
            throw new IllegalArgumentException("extra must be at least 1");
        }
        cups += extra;
    }
}

Now the only way to change cups is through addCups, which refuses bad input. The rule "cups is always at least 1" lives in one place and cannot be bypassed. You can later change how cups are stored without breaking any caller, because callers only ever saw the methods.

Getters (getCups) are common, but do not add a setter for every field by reflex. A public setCups(int) that accepts anything would reopen the door you just closed.

The shortcut

For every member, ask "Who truly needs this?" and pick the narrowest answer: only this class → private; its package → no keyword; subclasses → protected; the whole world → public. Start private and widen only when a real caller needs it.

Locking down the Order classStep 1 of 4
  1. The data

    cups is only changed by Order's own rules, so it becomes private.

Drills

Answer fast — trust the rule.

quick drill

Which specifier lets only code inside the same class use a member?

quick drill

A method in Order has no access keyword. Which other classes can call it?

quick drill

cups is private in Order. Another class writes order.cups = -3;. What happens?

quick drill

What is the main benefit of routing changes through addCups instead of a public field?

Remember

  • Make fields private; open only the doors you mean to.
  • Ask "who truly needs this?" and pick the narrowest level.
  • Traps: public fields, and a setter for everything that undoes the protection.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainProtect the loyalty balance

A LoyaltyCard class has a public int points field, and bugs elsewhere keep setting it negative. Explain how you would redesign it with access specifiers, which methods you would offer, and why this stops the bug.