link: OOP
Abstraction
Overview
Abstraction is a core concept in object-oriented programming that simplifies the management of complex systems by focusing on the essential characteristics of an object, while hiding the unnecessary implementation details. This principle helps developers build interfaces that are cleaner and more intuitive, enhancing both usability and maintainability.
Abstract
Abstraction is a general concept which you can find in the real world as well as in OOP language
Reduce Performance
A lot of level of abstractions can significantly reduce performance. Be careful with it and try to avoid a huge hierarchy of inheritance
Content
Abstraction allows programmers to handle complexity by providing a simplified model that highlights an object’s interactions without revealing the internal processes. It helps in reducing complexity and isolating impacts of changes, making the system easier to understand and modify.
Real-World Examples of Abstraction
-
Car Dashboard: The dashboard of a car gives the driver information about the car’s speed, fuel level, and warnings. It simplifies the driving process without requiring the driver to understand the complexities of the vehicle’s internal mechanics.
-
Remote Control: A remote control enables users to operate a television or other devices from a distance, using simple buttons to perform complex operations like changing channels or adjusting settings. The user interacts with the device through a simple interface, abstracted from the electronic complexities inside.
Implementing
Abstraction can be achieved with either abstract classes or Interface (Abstract Class vs Interface).
The abstract
keyword is used for classes and methods:
-
Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class).
-
Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the derived class (inherited from).
An abstract class can have both abstract and regular methods:
csharp
From the example above, it is not possible to create an object of the Animal class:
csharp
To access the abstract class, it must be Inherited from another class.
Types of Abstraction in OOP
Abstraction in OOP can primarily be categorized into two types:
Data Abstraction
Data Abstraction focuses on hiding the internal state and requiring users to interact with data through specified methods. A typical implementation involves abstract classes or interface
Example
In this example, the
Vehicle
class uses data abstraction by encapsulating thelicensePlate
attribute. Users of theCar
class interact with this data through a method (DisplayLicensePlate
), which internally calls another method that handles data formatting. This abstracts the data handling details from the user.
Process Abstraction
Process Abstraction simplifies complex operations by hiding the underlying implementation details and providing a user-friendly interface to perform the operations.
Example
In this example, the
DocumentProcessor
class abstracts the process of document analysis, which involves parsing, analyzing, and saving the document. The details of these operations are hidden, allowing users to simply callProcessDocument
to perform the entire process. This is a clear demonstration of process abstraction.
Key Differences Highlighted
- Data Abstraction: Focuses on encapsulating data within classes and exposing necessary operations to interact with this data, keeping the data manipulation details hidden.
- Process Abstraction: Involves hiding the steps involved in executing a complex process, allowing users to execute the process without knowing the internal steps.
Abstraction vs Encapsulation
Abstraction vs Encapsulation
Lets take a look on difference between 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.
No Abstraction Encapsulation 1. It is the process of gaining information. It is a method that helps wrap up data into a single module. 2. The problems in this technique are solved at the interface level. Problems in encapsulation are solved at the implementation level. 3. It helps hide the unwanted details/information. It helps hide data using a single entity, or using a unit with the help of method that helps protect the information. 4. It can be implemented using abstract classes and interfaces. It can be implemented using access modifiers like public, private and protected. 5. The complexities of the implementation are hidden using interface and abstract class. The data is hidden using methods such as getters and setters. 6. Abstraction can be performed using objects that are encapsulated within a single module. Objects in encapsulation don’t need to be in abstraction. Link to originalImportant
The most significant difference between the two is that data abstraction is a method which helps to hide the unwanted data from the user, while data encapsulation is a method which helps to hide data using a single entity.
Summary
Abstraction in programming is crucial for managing complexity. It allows developers to reduce intricate and detailed coding requirements into simpler, more manageable blocks, thereby improving the modularity and potential for reuse of code. Abstraction ensures that systems are not only manageable but also scalable and robust.
Summary
The strategic use of abstraction in software development enhances efficiency and error management, and is essential for creating flexible and sustainable code architectures.
References
”The Power of Abstraction” with Prof. Barbara Liskov - YouTube
Abstraction - w3school