1. Overview

In this tutorial, we’ll explain two essential concepts of OOP: abstraction and encapsulation. While similar in some ways, they’re different in others, and understanding these differences is fundamental.

2. Warming Up With a Real-World Example

Let’s suppose we are building a program to manage a small library. We could create a single class representing the books and the library and store all the relevant data within that class. We might expose all its attributes to the rest of the code so that it’s possible to change them from any other class.

While this approach might work for a small library, it would be problematic in two aspects:

  • it exposes the internal details
  • it quickly becomes unmanageable as the library grows larger and more complex

With abstraction and encapsulation, we could instead create separate classes to represent the books, the library, and the checkout system. Each class would have a well-defined interface that exposes only the necessary information and methods. Moreover, the implementation details would be hidden. Breaking down the system into smaller, more manageable components increases its flexibility, maintainability, and usability.

3. Abstraction

Abstraction means focusing on essential features of an object or a system while ignoring irrelevant details.

The most common form of abstraction in OOP is data abstraction, a handy form of modular programming. A set of abstract operations fully defines the behavior of an abstract data object. A user doesn’t need to understand how to implement these operations or how to represent the object. We achieve data abstraction using abstract classes and interfaces.

An abstract class is a class we can’t instantiate. We use abstraction to provide a common interface for related classes. Abstract classes can have both abstract and non-abstract methods. Abstract methods are those without an implementation. We implement them in the classes inheriting the abstract base class.

An interface is a collection of abstract methods and constants. They define a set of functionalities that a class must implement. So, an interface is like a contract a class agrees to fulfill so that other classes know how to use it and communicate with it. A class that implements an interface must implement all its methods.

4. Encapsulation

Encapsulation is a mechanism that packs data and methods into a single unit. It hides the implementation details of an object from the user. It also defines a straightforward interface for interacting with the object and helps achieve modularity, reusability, and data security.

In brief, developers typically achieve encapsulation using access modifiers, getters, and setters. They determine the level of access to a class, method, or variable.

For example, in Java, the private access modifier limits the access of an attribute or method to the class in which it is declared. To access such attributes, we use special methods we call getters and setters.

5. An Example

Let’s consider an OOP system for vending machines:

UML for Abstraction and Encapsulation

The abstract class VendingMachine represents a vending machine’s basic behavior. SodaVendingMachine and SnackVendingMachine implement specific vending devices for different products. They are child classes of VendingMachine. Additionally, an interface called IVendableItem represents any item we can get from a machine.

The VendingMachine class is an example of abstraction, as it defines the common characteristics of all vending machines without specifying the details of each individual. Moreover, the abstract class VendingMachine encapsulates the basic behavior of a vending machine, while the concrete classes SodaVendingMachine and SnackVendingMachine encapsulate and further specify the behaviors of specific types of machines. Finally, the VendableItem interface encapsulates the behavior of any item in a vending machine.

6. Differences Between Abstraction and Encapsulation

The following table compares them in terms of their similarities and differences:

Abstraction

Encapsulation

The process of hiding complex implementation details and exposing only the essential features of an object or system.

The process of hiding the internal details of an object or system and exposing only the necessary information to the outside world.

Focuses on the external behavior and functionality of an object or system.

Focuses on protecting the internal state and behavior of an object or system.

We achieve it using abstract classes, interfaces, and inheritance.

We implement it using classes, interfaces, and access modifiers such as private, public, protected, etc.

It allows developers to create simpler, more modular code that is easier to understand and maintain.

It allows developers to create more secure, robust code that is less prone to bugs and errors.

Useful for creating generic code that can be used in a variety of contexts.

Useful for creating objects and systems that can be used in complex environments without compromising their internal state or behavior.

Necessary for design patterns.

Enforces data integrity and prevents unauthorized access to sensitive information.

7. Conclusion

In this article, we talked about abstraction and encapsulation. Using them allows developers to write more efficient, reliable, and scalable code that’s easier to maintain and modify.

Due to their similarities, these terms are often used interchangeably but mean different things. So, it’s essential to know the difference: encapsulation is about hiding information, while abstraction is about hiding implementation.


« 上一篇: 查找前10个搜索词
» 下一篇: Telnet vs. SSH