Encapsulation in Java
Encapsulation is one of the four fundamental principles of object-oriented programming (OOP) and refers to the technique of hiding the implementation details of an object from its external users. In Java, encapsulation is achieved by declaring the instance variables of a class as private, which means that they can only be accessed within the same class.
To provide access to these private instance variables, Java provides public methods called getters and setters, which are used to retrieve and modify the values of the private variables, respectively. This way, the external users of the object can interact with it only through these public methods, without being able to access or modify its internal state directly.
Encapsulation has several benefits, such as:
- Enhancing the security of an object's data by preventing unauthorized access.
- Allowing the implementation details of an object to be changed without affecting its external users.
- Simplifying the code by reducing the number of dependencies between different parts of the program.
- Providing a clear and well-defined interface for interacting with an object, which improves the readability and maintainability of the code.
- Encapsulation is often referred to as the "data hiding" principle because it involves hiding the internal state of an object from the outside world.
- By using encapsulation, you can create more robust and reliable code. Because the internal state of an object is hidden, you can change the implementation of the class without worrying about breaking other parts of the code that depend on it.
- Encapsulation is closely related to the concept of abstraction, which involves hiding the implementation details of a system from its users. Together, abstraction and encapsulation provide a powerful way to manage complexity in large software projects.
- Encapsulation is not just about hiding data; it also involves hiding behavior. In other words, you can use encapsulation to hide the methods that are used to implement the behavior of an object. This can be useful when you want to prevent external code from directly modifying the behavior of an object.
- Encapsulation is not an all-or-nothing proposition. You can choose to encapsulate some parts of a class but not others, depending on your specific needs. For example, you might choose to encapsulate the data members of a class but leave the methods public, or vice versa.
- Encapsulation is not unique to Java; it is a fundamental concept in all object-oriented programming languages. However, Java's strict access modifiers (private, protected, and public) make it particularly easy to implement encapsulation.
public class Person { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
public class Main { public static void main(String[] args) { Person person = new Person(); person.setName("John Smith"); person.setAge(30); System.out.println(person.getName() + " is " + person.getAge() + " years old."); } }
public class BankAccount { private String accountNumber; private double balance; private String password; public BankAccount(String accountNumber, double balance, String password) { this.accountNumber = accountNumber; this.balance = balance; this.password = password; } public String getAccountNumber() { return accountNumber; } public double getBalance() { return balance; } public void deposit(double amount) { balance += amount; } public void withdraw(double amount, String password) { if (password.equals(this.password)) { if (amount <= balance) { balance -= amount; } else { throw new IllegalArgumentException("Insufficient funds"); } } else { throw new IllegalArgumentException("Invalid password"); } } }
public class Main { public static void main(String[] args) { BankAccount account = new BankAccount("1234567890", 1000.0, "mypassword"); System.out.println("Account number: " + account.getAccountNumber()); System.out.println("Initial balance: " + account.getBalance()); account.deposit(500.0); System.out.println("New balance after deposit: " + account.getBalance()); try { account.withdraw(200.0, "wrongpassword"); } catch (IllegalArgumentException e) { System.out.println("Failed to withdraw: " + e.getMessage()); } try { account.withdraw(2000.0, "mypassword"); } catch (IllegalArgumentException e) { System.out.println("Failed to withdraw: " + e.getMessage()); } account.withdraw(200.0, "mypassword"); System.out.println("New balance after withdrawal: " + account.getBalance()); } }
public class Employee { private String name; private int age; private double salary; public Employee(String name, int age, double salary) { this.name = name; this.age = age; this.salary = salary; } public String getName() { return name; } public int getAge() { return age; } public double getSalary() { return salary; } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public void setSalary(double salary) { this.salary = salary; } }
public class Main { public static void main(String[] args) { Employee employee = new Employee("John Smith", 30, 50000.0); System.out.println("Employee name: " + employee.getName()); System.out.println("Employee age: " + employee.getAge()); System.out.println("Employee salary: " + employee.getSalary()); employee.setAge(31); employee.setSalary(55000.0); System.out.println("Employee new age: " + employee.getAge()); System.out.println("Employee new salary: " + employee.getSalary()); } }