Java - OOP - Modifiers - Private

In Java, a modifier is a keyword that is used to modify the behavior of a class, method, or variable. One of the most common modifiers is the private modifier. When you declare a variable or method as private, it means that it can only be accessed within the same class.

The main advantage of using private modifiers is that it provides encapsulation, which means that the internal workings of the class are hidden from the outside world. This helps to prevent accidental or intentional misuse of the variables or methods within the class.

Let's look at an example to better understand the use of the private modifier:

public class BankAccount {
    private int balance;
    public BankAccount(int initialBalance) {
        balance = initialBalance;
    }

    public void deposit(int amount) {
        balance += amount;
    }

    public void withdraw(int amount) {
        if (balance < amount) {
            System.out.println("Insufficient balance!");
        } else {
            balance -= amount;
        }
    }

    public void displayBalance() {
        System.out.println("Balance: $" + balance);
    }
}

In this example, we have a BankAccount class that has a private instance variable balance. This variable can only be accessed within the BankAccount class, and not from any other class.

The BankAccount class also has three public methods: deposit(), withdraw(), and displayBalance(). These methods can be accessed from any other class, and they can manipulate the balance variable.

The deposit() method adds the specified amount to the balance variable, while the withdraw() method subtracts the specified amount from the balance variable, but only if the balance is sufficient. The displayBalance() method simply displays the current balance.

By making the balance variable private, we ensure that it cannot be accessed or modified from outside the BankAccount class. This prevents any external code from accidentally or intentionally modifying the balance, which could cause errors or security issues.

To access the balance variable from outside the BankAccount class, we would need to create a public getter method:

public int getBalance() {
    return balance;
}

This getter method allows us to retrieve the value of the balance variable, but it does not allow us to modify it. This maintains the integrity of the BankAccount class and ensures that the balance variable can only be modified by the deposit() and withdraw() methods.

public class Car {
   private String make;
   private String model;
   private int year;

   public Car(String make, String model, int year) {
      this.make = make;
      this.model = model;
      this.year = year;
   }

   private void startEngine() {
      System.out.println("The engine is started.");
   }

   public void drive() {
      startEngine();
      System.out.println("The car is driving.");
   }

   public String getMake() {
      return make;
   }

   public void setMake(String make) {
      this.make = make;
   }

   public String getModel() {
      return model;
   }

   public void setModel(String model) {
      this.model = model;
   }

   public int getYear() {
      return year;
   }

   public void setYear(int year) {
      this.year = year;
   }
}

In this example, we have a Car class that has three private attributes: make, model, and year. These attributes cannot be accessed directly from outside the class. We also have a private method startEngine() that is used internally by the drive() method.

To access or modify the values of the private attributes, we have defined public getter and setter methods. These methods can be accessed from outside the class to get or set the values of the attributes.

By making these attributes and methods private, we ensure that they cannot be accessed or modified directly from outside the class, and can only be accessed through the public methods provided. This is a good practice to ensure data encapsulation and information hiding in our code.

In summary, the private modifier is an important tool for encapsulating the internal workings of a class and preventing external code from accessing or modifying critical variables or methods. By using private modifiers, we can ensure that our code is more secure, maintainable, and reliable.