difference between abstraction and encapsulation in java

what is difference between abstraction and encapsulation in java

Abstraction and encapsulation are two fundamental principles of object-oriented programming, and while they are closely related, they serve different purposes.

Let’s discuss each concept and highlight the key differences with examples.

difference between abstraction and encapsulation in java

Abstraction:

Abstraction is the process of simplifying complex systems by modeling classes based on the essential properties and behaviors they exhibit. It involves creating abstract classes and interfaces that define the structure and behavior of a system without specifying the implementation details. Abstraction allows developers to focus on what an object does rather than how it achieves its functionality.

Example of Abstraction:

Example
// Abstract class defining the concept of a Shape
abstract class Shape {
    // Abstract method to calculate area (no implementation details)
    public abstract double calculateArea();
}

// Concrete class implementing the Shape abstraction
class Circle extends Shape {
    private double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    // Implementation of the abstract method
    @Override
    public double calculateArea() {
        return Math.PI * Math.pow(radius, 2);
    }
}

In this example, Shape is an abstract class that defines the concept of a shape and declares an abstract method calculateArea(). The Circle class is a concrete class that extends Shape and provides an implementation for the calculateArea method. The abstraction allows us to represent the common properties and behavior of various shapes without specifying the details of each shape’s implementation.

Encapsulation:

Encapsulation is the bundling of data (attributes) and methods (functions) that operate on the data into a single unit, known as a class. It involves hiding the internal details of how an object achieves its functionality and exposing only what is necessary for the outside world to interact with the object. Encapsulation is achieved by declaring the data members of a class as private and providing public methods (getters and setters) for controlled access.

Example of Encapsulation:

Example
// Class demonstrating encapsulation
public class BankAccount {
    private double balance;

    // Getter method for balance
    public double getBalance() {
        return balance;
    }

    // Setter method for balance with validation
    public void setBalance(double balance) {
        if (balance >= 0) {
            this.balance = balance;
        } else {
            System.out.println("Invalid balance. Cannot be negative.");
        }
    }
}

In this example, BankAccount encapsulates the concept of a bank account. The balance is a private data member, and access to it is controlled through public methods (getBalance and setBalance). The encapsulation ensures that the internal details of the bank account, such as how the balance is stored or updated, are hidden from external users. It provides controlled access to the balance and allows validation before modifying the data.

Key Differences:

1. Focus:

  • Abstraction: Focuses on modeling concepts and defining their essential properties and behaviors.
  • Encapsulation: Focuses on bundling data and methods into a single unit and controlling access to the internal details.

2. Implementation vs. Interface:

  • Abstraction: Deals with the abstraction of the interface, hiding implementation details.
  • Encapsulation: Deals with bundling the implementation details and controlling access through an interface.

3. Example:

  • Abstraction: Involves abstract classes and interfaces defining the structure and behavior without specifying implementation details.
  • Encapsulation: Involves classes bundling data and methods, controlling access to the data through getters and setters.

In summary, abstraction is about modeling concepts at a high level, while encapsulation is about bundling data and methods and controlling access to them. Both principles work together to create modular, maintainable, and scalable object-oriented systems.

Leave a Reply

Your email address will not be published. Required fields are marked *