In Java, access modifiers are keywords that determine the accessibility of classes, methods, and variables in an object-oriented program. There are four access modifiers in Java:

  1. public: Classes, methods, and variables declared as public can be accessed from anywhere in the program.
  2. private: Classes, methods, and variables declared as private can only be accessed within the same class.
  3. protected: Classes, methods, and variables declared as protected can be accessed within the same class, as well as within any subclasses (classes that extend the original class) and within the same package.
  4. default (also known as package-private): Classes, methods, and variables that have no access modifier specified (i.e., not marked as public, private, or protected) can only be accessed within the same package.

Access modifiers are used to control the level of encapsulation of an object-oriented program and to restrict access to sensitive or implementation-specific details of the program.

Here are some examples of how access modifiers are used in Java:

Public Access Modifier:

public class Car {
    public String make;
    public String model;

    public void startEngine() {
        // code to start the engine
    }
}

In this example, the Car class and its variables make and model are declared as public, meaning that they can be accessed from anywhere in the program. The startEngine method is also declared as public, meaning that it can be called from anywhere in the program.

Private Access Modifier:

public class BankAccount {
    private double balance;

    private void updateBalance(double amount) {
        balance += amount;
    }
}

In this example, the balance variable is declared as private, meaning that it can only be accessed within the same BankAccount class. The updateBalance method is also declared as private, meaning that it can only be called from within the same BankAccount class.

Protected Access Modifier:

public class Animal {
    protected String name;

    protected void makeSound() {
        // code to make a sound
    }
}

public class Dog extends Animal {
    public void bark() {
        System.out.println(name + " barks!");
        makeSound();
    }
}

In this example, the Animal class declares the name variable and the makeSound method as protected, meaning that they can be accessed within the same Animal class as well as within any subclass of Animal. The Dog class extends the Animal class and declares the bark method, which uses the name and makeSound methods inherited from the Animal class.

Default Access Modifier:

class Person {
    String name;
    int age;

    void sayHello() {
        System.out.println("Hello, my name is " + name + " and I am " + age + " years old.");
    }
}

In this example, the Person class does not declare any access modifiers for its name, age, or sayHello methods, meaning that they are only accessible within the same package. This is the default access level for classes, methods, and variables in Java if no access modifier is specified.