Access modifiers control visibility and accessibility of classes, methods and variables in Java.

Access Levels

ModifierClassPackageSubclassOutside
public
protected
(default)*
private
*No modifier = package-private (default)

Example

public class Car {
    public String brand;  // accessible everywhere
    protected int speed;  // accessible in subclasses or same package
    String model;         // default - package-only access
    private int engineNo; // only within this class
    
    private void startEngine() { ... }
    public drive() { ... }
}

Where They Apply

ModifierClassFieldMethodConstructor
public
protected
(default)
private

Real-World analogy

Think of access modifiers like locks on doors:

  • public: open to everyone,
  • protected: only family (package/subclass),
  • default: only people in your building (package),
  • private: only you.

Tip

Expect scenarios like:

  • can a private method be inherited? No
  • can protected fields be accessed in other packages? Yes, only if subclassed

Parent: _Core