Encapsulation vs Abstraction in Java

1. Introduction

Encapsulation in Java is the technique of bundling the data (variables) and code acting on the data (methods) together as a single unit. It restricts direct access to some of an object's components, which can prevent the accidental modification of data. Encapsulation is achieved using access modifiers, allowing you to control the visibility of class members. 

Abstraction in Java is a concept that aims to hide the complex reality while exposing only the necessary parts. It is achieved using abstract classes and interfaces. Abstraction lets you focus on what an object does instead of how it does it, facilitating a higher level of program complexity management. It serves as a foundation for modelling complex systems by simplifying object interactions.

2. Key Points

1. Encapsulation hides the internal state and requires all interaction through an object's methods.

2. Abstraction hides the implementation details and only shows the user the necessary functionality.

3. Encapsulation is achieved in Java using private, protected, and public access modifiers.

4. Abstraction is achieved in Java using abstract classes and interfaces.

3. Differences

Encapsulation Abstraction
The process of wrapping code and data into a single unit, such as a Java class. The concept of hiding the complex implementation details and showing only the necessary features of an object.
This is achieved in Java by restricting access to class members using access modifiers like private, public, protected, and default. Achieved in Java using abstract classes and interfaces.
Encapsulation is used to protect a class's data (variables) from unauthorized access and modification. Abstraction is used to hide the complex implementation details and show only the necessary functionalities to the user.
It focuses on controlling access to data and the methods of an object to ensure integrity and security. It focuses on reducing complexity by hiding details and exposing only the essential parts of a system.
In encapsulation, data hiding is achieved by making a class's variables private and providing public getter and setter methods to modify and view the variables' values. In abstraction, complexity is managed by providing abstract interfaces that represent complex entities more straightforwardly.
Example: A capsule tightly encapsulates several combinations of medicines. Example: A TV remote abstracts the complex electronics inside the TV and provides the user with a simple interface.

4. Example

// Example of Encapsulation
public class Employee {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

// Example of Abstraction
public abstract class Animal {
    public abstract void makeSound();

    public void eat() {
        System.out.println("Animal is eating");
    }
}

public class Dog extends Animal {
    @Override
    public void makeSound() {
        System.out.println("Woof woof");
    }
}

Explanation:

1. The Employee class uses encapsulation by keeping its name field private and providing public getter and setter methods.

2. The Animal class demonstrates abstraction by providing a method eat that has an implementation and an abstract method makeSound that does not have an implementation.

5. When to use?

Encapsulation 

Data Protection: Use encapsulation when you need to protect the integrity of your data by restricting access to it. By making class fields private and providing public getter and setter methods, you can control how the data is accessed or modified. Encapsulation is crucial to prevent unauthorized or unintended access and ensure that objects are always in a valid state. Modularity and 

Maintenance: Encapsulation increases the modularity of your code, making it easier to change and maintain. Since the implementation details are hidden behind a well-defined interface (getters and setters), changes to the implementation do not affect the external code that relies on the object. This makes your code more flexible and more accessible to update, as you can modify the internal workings of a class without impacting its consumers. 

Abstraction 

Complexity Reduction: Use abstraction to simplify complex real-world problems by modelling classes based on the essential, inherent aspects of the problem while ignoring the irrelevant details. Abstraction is key in managing complexity in large software projects. It allows you to focus on what an object does instead of how it does it, facilitating a higher level of programming. 

Code Reusability and Scalability: Abstraction enables the design of a common set of protocols that different classes can implement uniquely. Defining abstract classes or interfaces creates a blueprint for other classes to follow, promoting code reusability and scalability. This approach is particularly useful when multiple classes should adhere to a certain contract or when you want to enforce a particular set of behaviors across different parts of an application.

Comments