Skip to content
Home » Blog » Encapsulation and Abstraction | The Complete Guide

Encapsulation and Abstraction | The Complete Guide3 min read

Encapsulation and Abstraction

When it comes to Object Oriented Programming (OOPS), Two essential concepts stand out: Encapsulation and Abstraction. These principles play a pivotal role in designing clean, maintainable, and efficient code. In this post, we will delve into the world of encapsulation and abstraction. To get a deeper view of these concepts we will use some Java code examples that showcase their real-world applications.

Also Read: Top 10 Programming Languages to Learn in 2024 | Future-Proof Programming Languages

What is Encapsulation?

Encapsulation is like a protective shield around your data and the methods that manipulate it. Think of it as a fortress of valuable treasures. It allows you to hide the complexity of your data and control access to it. Let’s see an example of encapsulation in Java:

public class BankAccount {
    private double balance;

    public void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
        }
    }

    public void withdraw(double amount) {
        if (amount > 0 && amount <= balance) {
            balance -= amount;
        }
    }

    public double getBalance() {
        return balance;
    }
}

In the above code, the balance variable is encapsulated within the BankAccount class. It’s marked as private meaning it can only be accessed and modified from within the class itself. The deposit and withdraw methods provide controlled access to the balance ensuring that it’s always updated correctly and never goes below zero.

What is Abstraction?

On the other hand, Abstraction is like viewing an object from a distance, focusing only on the important details while ignoring the irrelevant ones. It allows you to create simplified models of complex systems. Abstraction helps manage complexity by exposing only what’s necessary. Let’s look at a Java example to illustrate abstraction:

Also Read: Android Development Roadmap | A Guide to Start Android Development

public abstract class Shape {
    public abstract double calculateArea();
}

In the above code, we have an abstract class Shape. It defines a method calculateArea() but doesn’t provide an implementation. Subclasses like Circle or Rectangle must extend Shape and provide their own implementations of calculateArea(). This way, we abstract the common property of all shapes: the ability to calculate their area, while leaving the specific calculations to individual shapes.

Difference between Abstraction and Encapsulation

EncapsulationAbstraction
It primarily focuses on controlling access to data and methods, ensuring they are used correctly.It concentrates on simplifying complex systems, allowing developers to work with high-level concepts and hide implementation details.
Encapsulation is concerned with managing data and protecting it from unauthorized access or modification.Abstraction deals with creating simplified models that capture the essential characteristics of an object or system.
Encapsulation is about how you implement the details of a class, making sure data integrity is maintained.Abstraction is about designing a system or class hierarchy to represent concepts and relationships.

Conclusion

Encapsulation and abstraction are fundamental principles of object-oriented programming that, when used thoughtfully, can lead to clean, maintainable, and, efficient code. Encapsulations shield your data and methods, while abstraction simplifies complex systems. By understanding these concepts and applying them appropriately in your code, you can become a more skilled and effective programmer, producing code that is both robust and easy to work with.

Tanmay Sinha

1 thought on “Encapsulation and Abstraction | The Complete Guide3 min read

  1. Pingback: What is System Design? An Introduction to System Design - Mr Programmer

Leave a Reply