Encapsulation Explanation - Notes by Shariq SP
Understanding Encapsulation in Java
Encapsulation is one of the fundamental principles of Object-Oriented Programming (OOP) in Java. It refers to the bundling of data and methods that operate on that data within a single unit, called a class. Encapsulation helps in achieving data hiding, abstraction, and modularity, which are essential for building robust and maintainable software systems.
What is Encapsulation?
Encapsulation involves:
- Declaring the instance variables of a class as private to restrict direct access from outside the class.
- Providing public getter and setter methods to access and modify the private variables.
- Performing validation and ensuring data consistency within the setter methods.
Encapsulation in Action
Encapsulation is commonly used in scenarios where:
- Sensitive data needs to be protected from unauthorized access or modification.
- Data consistency and integrity need to be maintained through controlled access.
- Modular and maintainable code is desired by encapsulating related data and behaviors within a class.
How to Achieve Encapsulation
To achieve encapsulation in Java:
- Declare the instance variables of a class as private.
- Provide public getter methods (accessors) to retrieve the values of private variables.
- Provide public setter methods (mutators) to modify the values of private variables.
Example 1: Encapsulation
Here's an example of encapsulation in Java:
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) {
if (age >= 0 && age <= 120) {
this.age = age;
} else {
System.out.println("Invalid age value");
}
}
}
Example 2: Encapsulation
Another example demonstrating encapsulation:
public class BankAccount {
private double balance;
public double getBalance() {
return balance;
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
}
}
}
Interview Questions on Encapsulation
- What is encapsulation in Java?
- Why is encapsulation important in Object-Oriented Programming?
- What are the benefits of encapsulation?
- How is encapsulation achieved in Java?
- What are access modifiers, and how are they related to encapsulation?
Multiple Choice Questions (MCQs) - Encapsulation
- Which of the following statements best describes encapsulation?
- Exposing all class members as public for easy access
- Bundling of data and methods within a single unit
- Using inheritance to derive new classes from existing ones
- Providing multiple entry points to a method
- Why are instance variables of a class often declared as private?
- To allow direct access from outside the class
- To restrict access and ensure data integrity
- To increase code complexity
- To enable polymorphism
- Which keyword is used to restrict access to a class member in Java?
- public
- private
- protected
- default
- What is the purpose of getter methods in encapsulation?
- To modify the state of an object
- To provide controlled access to private variables
- To perform validation of input data
- To create new instances of a class
- Which access modifier is used to make a class member accessible only within its own class?
- public
- private
- protected
- default