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

  1. What is encapsulation in Java?
  2. Why is encapsulation important in Object-Oriented Programming?
  3. What are the benefits of encapsulation?
  4. How is encapsulation achieved in Java?
  5. What are access modifiers, and how are they related to encapsulation?

Multiple Choice Questions (MCQs) - Encapsulation

  1. Which of the following statements best describes encapsulation?
    1. Exposing all class members as public for easy access
    2. Bundling of data and methods within a single unit
    3. Using inheritance to derive new classes from existing ones
    4. Providing multiple entry points to a method
  2. Why are instance variables of a class often declared as private?
    1. To allow direct access from outside the class
    2. To restrict access and ensure data integrity
    3. To increase code complexity
    4. To enable polymorphism
  3. Which keyword is used to restrict access to a class member in Java?
    1. public
    2. private
    3. protected
    4. default
  4. What is the purpose of getter methods in encapsulation?
    1. To modify the state of an object
    2. To provide controlled access to private variables
    3. To perform validation of input data
    4. To create new instances of a class
  5. Which access modifier is used to make a class member accessible only within its own class?
    1. public
    2. private
    3. protected
    4. default