DEV Community

Cover image for Understanding Abstraction in OOP
mahdi
mahdi

Posted on

Understanding Abstraction in OOP

Explanation:

Abstraction is a fundamental concept in computer science and software engineering, especially within the object-oriented programming paradigm Examples of this include:
the usage of abstract data types to separate usage from working representations of data within programs;

Rationale

Computing mostly operates independently of the concrete world. The hardware implements a model of computation that is interchangeable with others. The software is structured in architectures to enable humans to create the enormous systems by concentrating on a few issues at a time. These architectures are made of specific choices of abstractions.

Language abstraction is a central form of abstraction in computing: new artificial languages are developed to express specific aspects of a system. Modeling languages help in planning. Computer languages can be processed with a computer. An example of this abstraction process is the generational development of programming language from the machine language to the assembly language and the high-level language. Each stage can be used as a stepping stone for the next stage. The language abstraction continues for example in scripting languages and domain-specific programming languages.

Within a programming language, some features let the programmer create new abstractions. These include subroutines, modules, polymorphism, and software components. Some other abstractions such as software design patterns and architectural styles remain invisible to a translator and operate only in the design of a system.

In object-oriented programming languages such as C++ or Java, the concept of abstraction itself has become a declarative statement - using the syntax function (parameters) = 0. (in C++) or the keywords abstract and interface (in Java).

Summary:

Abstraction is one of the 4 pillars of object-oriented programming. The main purpose of abstraction is to hide the implementation details and show only necessary/necessary information. Simply put, it reduces complexity for the average user. It is mainly used to solve problems at the design level, mainly the conceptual part.

Abstraction in real life

  • Your car is a great example of abstraction. You can start the car by turning the key or pressing the start button. You don't need to know how the engine starts, what components your car has. The internal implementation and complex logic of the car is completely hidden from the user.

  • We can heat our food in the microwave. We press a few buttons to set the timer and the type of food. Finally, we eat a warm and delicious meal. The internal details of the microwave are hidden from us. We have accessed the functionality in a very simple way.

Understanding Abstraction in OOP

Abstraction, in the context of OOP, refers to the ability to hide complex implementation details and show only the necessary features of an object. This simplifies the interaction with objects, making programming more intuitive and efficient. It provides a clear separation between what an object does and how it achieves its functionality, fostering a higher level of understanding and collaboration among developers.

Abstraction in JavaScript

tip: In Javascript, the concept of an abstract class is not natively supported as it is in other languages such as Java, TypeScript, and Python. However, we can write custom code to mimic the behavior of an abstract class in Javascript.

Different Types of Abstraction

There are primarily two types of abstraction implemented in OOPs. One is data abstraction which pertains to abstracting data entities. The second one is process abstraction which hides the underlying implementation of a process. Let’s take a quick peek into both of these.

Types of Abstraction in OOP

  1. Data Abstraction

Data abstraction is a fundamental concept in Object-Oriented Programming (OOP) that allows developers to manage complexity by focusing on essential features while hiding implementation details. At its core, data abstraction involves the creation of abstract data types that represent real-world entities or concepts.

Imagine you're working with a complex system like a car. In OOP, you would represent the car as an object with properties such as make, model, year, color, and methods such as start, stop, accelerate, and brake. However, you don't need to know the intricate details of how each of these methods is implemented within the car's internal systems. Instead, you interact with the car object using its interface (methods), abstracting away the complexities of its internal mechanisms.

For exapme (real life), let's say you decide to upgrade your car to a newer model. When you switch to the newer model, you're essentially interacting with a higher level of abstraction. You still have a car object, but now it might have additional features, improved performance, and better efficiency. However, as a user of the car object, you don't need to concern yourself with the specifics of how these improvements are implemented under the hood. You can simply use the car object's methods to perform tasks like driving, without worrying about the intricacies of its internal workings.

Data abstraction allows programmers to define abstract data types that encapsulate the essential properties and behaviors of real-world entities. By hiding the implementation details, data abstraction promotes modular design and code reusability. It also facilitates easier maintenance and extension of the system, as changes to the underlying implementation can be made without affecting other parts of the codebase.

Encapsulation, another key principle of OOP, complements data abstraction by bundling data and methods together within objects and restricting access to the internal state of objects. This not only enhances security but also promotes the principle of information hiding, which is essential for effective data abstraction.

In summary, data abstraction in OOP involves presenting a simplified view of complex data structures and operations, allowing developers to focus on essential features while hiding implementation details. This promotes modularity, code reusability, and ease of maintenance, making it a crucial concept in software development.

Data Abstraction Example:

// Define a Car class with properties and methods
class Car {
  constructor(make, model, year, color) {
    this.make = make;
    this.model = model;
    this.year = year;
    this.color = color;
    this.isStarted = false; // Represents whether the car is started or not
    this.currentSpeed = 0; // Represents the current speed of the car
  }

  // Method to start the car
  start() {
    this.isStarted = true;
    console.log("The car is started.");
  }

  // Method to stop the car
  stop() {
    this.isStarted = false;
    this.currentSpeed = 0; // Reset the current speed when the car stops
    console.log("The car is stopped.");
  }

  // Method to accelerate the car
  accelerate(speedIncrement) {
    if (this.isStarted) {
      this.currentSpeed += speedIncrement;
      console.log(
        `The car is accelerating. Current speed: ${this.currentSpeed} km/h`
      );
    } else {
      console.log("Cannot accelerate. Start the car first.");
    }
  }

  // Method to brake the car
  brake() {
    if (this.currentSpeed > 0) {
      this.currentSpeed -= 10; // Assuming a constant deceleration rate
      console.log(
        `The car is braking. Current speed: ${this.currentSpeed} km/h`
      );
    } else {
      console.log("The car is already stopped.");
    }
  }
}

// Create a new instance of the Car class
const myCar = new Car("Toyota", "Camry", 2022, "silver");

// Now, let's interact with the car object using its methods
myCar.start(); // Output: The car is started.
myCar.accelerate(50); // Output: The car is accelerating. Current speed: 50 km/h
myCar.brake(); // Output: The car is braking. Current speed: 40 km/h
myCar.stop(); // Output: The car is stopped.
Enter fullscreen mode Exit fullscreen mode
  1. Process Abstraction

Process abstraction in software development refers to hiding the underlying operational details of a process while exposing only the essential methods or actions that can be performed. This abstraction allows developers to work with abstract processes, utilizing hidden processes internally to achieve desired functionality.

Let's elaborate on the elevator example to understand process abstraction better:

When you're inside an elevator and you press a button to go to a desired floor, you're interacting with a high-level abstraction of the elevator's functionality. You simply press a button corresponding to the floor you want to reach, without needing to know the intricate details of how the elevator moves, accelerates, decelerates, and stops at the selected floor.

The process of going to the desired floor involves numerous complex operations within the elevator system, such as controlling the motor, managing the doors, monitoring the position of the elevator, and coordinating with other systems like sensors and safety mechanisms. However, as a user of the elevator, you're shielded from these low-level details. You only need to understand and utilize the abstract process of pressing a button to reach your destination.

In terms of software development, process abstraction allows developers to define methods or functions that represent high-level actions or operations without exposing the implementation details. For example, in an elevator control system implemented in software, you might have methods like goToFloor(floorNumber) or openDoor(), which abstract the process of elevator movement and door operation, respectively. These methods encapsulate the underlying complexities of elevator control, providing a simplified interface for interaction.

By employing process abstraction, developers can design systems that are easier to understand, maintain, and extend. It promotes modularity, code reusability, and separation of concerns, as different parts of the system can interact with abstract processes without being tightly coupled to the implementation details. Additionally, process abstraction enhances readability and reduces complexity, making software more comprehensible and manageable for both developers and users.

Process Abstraction Example:

// Define an Elevator class representing an elevator object
class Elevator {
  constructor(currentFloor) {
    this.currentFloor = currentFloor; // Current floor of the elevator
  }

  // Method to go to a specified floor
  goToFloor(floorNumber) {
    console.log(
      `Elevator moving from floor ${this.currentFloor} to floor ${floorNumber}`
    );
    this.currentFloor = floorNumber; // Update current floor
    console.log(`Elevator arrived at floor ${this.currentFloor}`);
  }

  // Method to simulate opening the elevator door
  openDoor() {
    console.log("Elevator door is opening...");
    // Simulate door opening time (setTimeout used for demonstration purposes)
    setTimeout(() => {
      console.log("Elevator door is open.");
    }, 1000); // Simulate door opening time
  }

  // Method to simulate closing the elevator door
  closeDoor() {
    console.log("Elevator door is closing...");
    // Simulate door closing time (setTimeout used for demonstration purposes)
    setTimeout(() => {
      console.log("Elevator door is closed.");
    }, 1000); // Simulate door closing time
  }
}

// Create a new instance of the Elevator class
const myElevator = new Elevator(1);

// Interact with the elevator using its abstracted methods
myElevator.goToFloor(5); // Output: Elevator moving from floor 1 to floor 5, Elevator arrived at floor 5
myElevator.openDoor(); // Output: Opening elevator door..., Elevator door opened.
myElevator.closeDoor(); // Output: Closing elevator door..., Elevator door closed.
Enter fullscreen mode Exit fullscreen mode

Abstraction vs Encapsulation

A lot of times programmers often confuse abstraction with encapsulation because in reality the two concepts are quite intertwined and share a relationship between them. Abstraction, as we’ve seen pertains to hiding underlying details and implementation in a program. Encapsulation, on the other hand, describes how abstraction occurs in a program.

Abstraction is a design-level process but encapsulation is an implementation process. Encapsulation tells us how exactly you can implement abstraction in the program. Abstraction pertains to only displaying the essential details to the user whereas encapsulation pertains to typing up all the data members and associated member functions into a single abstracted unit.

Aspect Abstraction Encapsulation
Definition Hides unnecessary details and presents only essential features. Bundles data and methods into a single unit, restricting access to the internal state.
Focus Design-level concept Implementation-level concept
Purpose Simplify complex systems, focusing on what matters. Ensure data integrity and provide a clear interface for interaction.
What it hides Hides implementation details Hides internal state and implementation details
Level of Detail Displays essential features only Bundles data and methods together, abstracting the implementation
Use Case Providing a simplified view of complex systems. Protecting data and providing a clear interface for interaction.
Implementation Achieved through interfaces, abstract classes, and inheritance. Achieved through access modifiers (public, private, protected) and classes.
Dependency Can exist without encapsulation, but often used together. Often used in conjunction with abstraction to achieve modular and maintainable code.

Example in JavaScript

Let's go back to the car example:

// Encapsulation: Car class encapsulates data and methods related to a car
class Car {
  constructor(make, model, year) {
    this.make = make; // Encapsulated attribute
    this.model = model; // Encapsulated attribute
    this.year = year; // Encapsulated attribute
    this.isStarted = false; // Encapsulated attribute
    this.currentSpeed = 0; // Encapsulated attribute
  }

  // Encapsulation: Methods operate on encapsulated attributes
  start() {
    if (!this.isStarted) {
      this.isStarted = true;
      console.log("The car has started.");
    } else {
      console.log("The car is already started.");
    }
  }

  accelerate(speed) {
    if (this.isStarted) {
      this.currentSpeed += speed;
      console.log(
        `The car is accelerating. Current speed: ${this.currentSpeed} km/h`
      );
    } else {
      console.log("Cannot accelerate. Start the car first.");
    }
  }

  stop() {
    if (this.isStarted) {
      this.isStarted = false;
      this.currentSpeed = 0;
      console.log("The car has stopped.");
    } else {
      console.log("The car is already stopped.");
    }
  }

  // Abstraction: Method abstracts the concept of driving without exposing implementation details
  drive(destination) {
    console.log(`Driving to ${destination}.`);
    this.start(); // Abstraction: Calling encapsulated method to start the car
    this.accelerate(60); // Abstraction: Calling encapsulated method to accelerate the car
    // Logic for driving to the destination
    this.stop(); // Abstraction: Calling encapsulated method to stop the car
  }
}

// Abstraction: Using the Car class without knowing its internal implementation details
const myCar = new Car("Toyota", "Camry", 2022);
myCar.drive("Work");
Enter fullscreen mode Exit fullscreen mode

In this example:

  • Car class encapsulates attributes like make, model, year, isStarted, and currentSpeed, along with methods like start, accelerate, and stop.
  • drive method abstracts the concept of driving to a destination without revealing the internal implementation details of starting, accelerating, and stopping the car.
  • myCar.drive('Work') demonstrates abstraction by using the drive method without needing to understand how the car actually starts, accelerates, or stops.

So, in summary:

  • Encapsulation: The Car class serves as the capsule, encapsulating data (attributes) and methods (functions) related to a car.
  • Abstraction: The drive method represents abstraction by providing a high-level interface for driving a car without exposing the underlying complexities of starting, accelerating, and stopping the car

Why Abstraction is important :

Abstraction is necessary because it simplifies complex tasks.

  1. Simplicity: Abstraction simplifies complex tasks by hiding unnecessary details and presenting only the essential features of an object or system. This makes code easier to understand, maintain, and debug.

  2. Modularity: Abstraction facilitates modularity by encapsulating implementation details within objects or modules. This separation of concerns allows for better organization, reusability, and easier maintenance of code.

  3. Code Reusability: Abstraction encourages the creation of reusable components, such as abstract classes or interfaces, which can be leveraged across multiple parts of the system or in different projects altogether. This promotes efficiency and consistency in software development, as developers can leverage existing solutions rather than reinventing the wheel.

  4. Time Saving: Abstraction saves time by providing high-level interfaces that abstract away complex implementation details. Developers can focus on using these interfaces rather than worrying about how they are implemented, leading to faster development cycles.

  5. Reduced Errors: By hiding implementation details and providing clear interfaces, abstraction reduces the likelihood of errors in code. Developers can rely on well-defined abstractions without needing to understand every intricate detail of their implementation.

  6. Focus on What Matters: Abstraction allows developers to focus on the main task at hand, rather than getting bogged down in irrelevant details. This increases productivity and ensures that efforts are directed towards solving the core problems of the project.

  7. Managing Complexity: In large projects, abstraction is essential for managing complexity. By breaking down the system into smaller, more manageable components, abstraction helps developers navigate through the codebase more easily and understand the relationships between different parts of the system.

Best Practices for Using Abstraction in OOP

1. Design Patterns and Abstraction:

  • Utilize Design Patterns: Design patterns like Factory Pattern, Strategy Pattern, and others often rely on abstraction to solve common design problems efficiently. Familiarize yourself with these patterns and use them where appropriate.

2. Tips for Effective Abstraction Implementation:

  1. Keep it Simple: Abstraction should simplify complexity, not introduce it. Aim for clear and maintainable abstractions that are easy to understand.

  2. Follow Naming Conventions: Use meaningful names for abstract classes, methods, and interfaces to enhance readability and comprehension. Clear naming helps convey the purpose and behavior of the abstraction.

  3. Favor Composition over Inheritance: Prefer composition over inheritance when designing abstract components. Composition promotes flexibility, reduces coupling, and avoids the limitations of class hierarchies.

  4. Strive for Reusability: Design abstractions with reusability in mind. Create components that can be easily reused and extended in various contexts without requiring modifications. This promotes code reuse and simplifies maintenance.

3. Handling Abstraction in Large and Complex Projects:

  • Maintain Consistency: In large projects, consistency in abstraction is crucial for maintainability and collaboration. Establish and adhere to coding standards and conventions to ensure uniformity across the codebase.

  • Documentation: Proper documentation of abstractions, including their purpose, usage, and contracts, helps developers understand and use them effectively. Document interfaces, abstract classes, and significant design decisions related to abstraction.

  • Code Reviews: Conduct regular code reviews to ensure that abstraction is applied correctly and consistently throughout the project. Code reviews provide an opportunity to identify potential issues, improve code quality, and enforce best practices related to abstraction.

  • Collaborative Communication: Encourage collaborative communication among team members to discuss and refine abstractions. Foster a culture of sharing knowledge and ideas to leverage collective expertise in designing and implementing abstractions effectively.

Conclusion:

As a result, abstraction is a powerful tool in software development that simplifies complexity by hiding unnecessary details and presenting only the essential features of objects or systems. This allows developers to focus on what matters, promote modularity, code reusability, and manage complexity effectively.

By following best practices for abstraction, such as using design patterns, implementing effective naming conventions, striving for reusability, and managing abstraction across large projects, developers can harness the full potential of abstraction to create elegant, efficient, and maintainable code bases. .

In short, abstraction is not just a concept, but a fundamental principle that shapes the way software systems are designed, implemented, and maintained, ultimately leading to better quality software and increased developer productivity.

Get in Touch:

If you have questions, feedback, or suggestions about the content of this article, or if you think there is an inaccuracy that needs to be addressed, please contact me. Your comments are valuable and help improve the quality and accuracy of the information provided.

You can contact me on twitter. to discuss any concerns or insights you may have. I am always open to constructive discussions and welcome the opportunity to work together to improve content for the benefit of all readers.

Thank you for your interest and participation!

Top comments (1)

Collapse
 
abhi11 profile image
Abhishek Kumar

Well description provided

Nice i appreciate