Abstraction vs Encapsulation
What motivated me to write this article?
I found it to be very difficult to distinguish between Abstraction and Encapsulation, because the concepts are intertwingled. Now the Question comes why I want to distinguish. I want to distinguish them because they are the very first concept of OOP. So having an ambiguity at the very beginning weakens the foundation. Therefore, I want to remove the ambiguity by uncovering the layers. I believe there should be standard unambiguous definitions accepted by all OOP languages for OOP, Class, Objects, Attributes, Behaviours, methods, Properties, Abstraction, Encapsulation, Inheritance, Polymorphism and Access Modifiers
Let’s see some of the definitions of abstraction I found from Internet.
Abstraction is the process of hiding the internal details of an application from the outer world.
Objects in an OOP language provide an abstraction that hides the internal implementation details.
Abstraction refers to providing only essential information about the data to the outside world, hiding the background details or implementation.
Similarly, let’s find out what internet says about Encapsulation
Encapsulation is defined as the wrapping up of data under a single unit. It is the mechanism that binds together code and the data it manipulates. In Encapsulation, the variables or data of a class are hidden from any other class and can be accessed only through any member function of their class in which they are declared. As in encapsulation, the data in a class is hidden from other classes, so it is also known as data-hiding.
Let’s see via the perspective of Alan Kay
Alan Kay coined the term “object oriented programming” at grad school in 1966 or 1967.
In a 2003 email exchange, Alan Kay clarified what he meant when he called Smalltalk “object-oriented”: “OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things.”
“I made up the term ‘object-oriented’, and I can tell you I didn’t have C++ in mind.” ~ Alan Kay, OOPSLA ‘97
The big idea was to use encapsulated mini-computers in software which communicated via message passing rather than direct data sharing — to stop breaking down programs into separate “data structures” and “procedures”.
Modern Object-Oriented programming language came later and have interpreted and implemented OOP in their own perspective. But I believe the underlying principles/pillars remain same. Principles vs pillars is again a mater of discussion but let’s do it some other day.
Let’s Decipher what Alan Kay said by breaking his statement, “OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things.”
Messaging : I am not very sure but it seems to me that modern programming languages have achieved it via Inheritance.
Let’s discuss inheritance. What does inheritance mean in English. The reception of genetic qualities by transmission from parent to child. In terms of programming, we can say, Inheritance is the creation of new classes (subclasses) based on existing classes (superclasses). Subclasses inherit attributes and methods from their superclasses, allowing code reuse and promoting the "is-a" relationship between classes.
local retention and protection and hiding of state-process: Seems like the modern programming languages have implemented it via Encapsulation.
What is Encapsulation?
Encapsulation is the process of bundling data (attributes) and methods (functions) into a single unit called a class, hiding the internal implementation details. It protects the data from unauthorized access and ensures that the class's methods are the only way to interact with the data. Encapsulation ensures data integrity, enhances code maintainability, and enables information hiding, preventing direct manipulation of internal implementation details.
extreme late-binding of all things : It seems like Modern Programming languages have implemented it via Polymorphism. Polymorphism means "many forms" and refers to the ability of objects to take on different forms based on their context. Polymorphism allows objects of different classes to be treated as instances of a common superclass. It enables methods to be implemented differently in different subclasses while adhering to a common interface. Polymorphism promotes code flexibility and extensibility and enables the implementation of methods specific to each object type. It enables the use of a common interface to interact with different objects, regardless of their specific implementations. Polymorphism is achieved through method overriding and method overloading, enabling different implementations of methods with the same name.
Now comes the question, where is Abstraction?
First let’s discuss what is Abstraction in General.
Abstraction means an idea in theory that can be implemented later or generalization of aspects of reality. Specifications can be provided later in reality. Abstraction can be referred to as modeling and is closely related to the concepts of theory and design.
I understand that “Abstraction in Programming” is different from “Abstraction in General”. If we assume “Abstraction in General” as Fruit, we may be talking about apple or orange, when we are talking about “Abstraction in programming”. Because we can say Apple is a fruit. But we would not be talking about Apple Pie when we are talking about “Abstraction in Programming”, considering “Abstraction in General” is a fruit. Because Apple Pie cannot be considered as Fruit. Same way when we say “Abstraction in Programming” is a process of data-hiding, we are actually saying the “process of making apple pie” is a fruit. I agree, we can make apple pie from apple. Same way we can achieve “data-hiding” via “Abstraction in Computers”. But apple pie is not an apple or a fruit. Similarly, Abstraction is neither data-hiding nor it is the process of data-hiding.
So, Abstraction is an underlying principle through which we can achieve data-hiding via encapsulation and access modifiers. Abstraction is not only about data-hiding or I will emphasise Abstraction is not at all about data-hiding. Data-hiding is one of the applications of Abstraction. Encapsulation provides data-hiding. Encapsulation itself is not sufficient for Data Hiding. Encapsulation is the process of bundling data (attributes) and methods (functions) into a single unit called a class, hiding the internal implementation details. Encapsulation hides the internal implementation details not the data. Encapsulation with the help of Access modifiers, hides the Data.
With reference to https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/tutorials/oop,
Abstraction Modeling the relevant attributes and interactions of entities as classes to define an abstract representation of a system.
Inheritance Ability to create new abstractions based on existing abstractions.
Polymorphism Ability to implement inherited properties or methods in different ways across multiple abstractions.
I am somehow not completely satisfied with definition of encapsulation mentioned here so I didn’t included it
In conclusion, Abstraction focuses on representing the essential characteristics of an object. Abstraction simplifies complex systems, promotes code modularity, and enhances code maintainability.
Encapsulation, Inheritance and Polymorphism are the applications or extensions of Abstraction. So, Abstraction is the underlying principle behind the core pillars of OOP i.e. Encapsulation, Inheritance and Polymorphism.
So I am more satisfied with following Definitions
Abstraction Modeling the relevant attributes and interactions of entities as classes to define an abstract representation of a system.
Inheritance Ability to create new abstractions based on existing abstractions.
Polymorphism Ability to implement inherited properties or methods in different ways across multiple abstractions.
Encapsulation Process of bundling data (attributes) and methods (functions) into a single unit called a class, hiding the internal implementation details.
Access Specifiers/Modifiers It protects the data from unauthorized access and ensures that the class's methods are the only way to interact with the data.
Data-hiding Can be achieved via Encapsulation + Access Specifiers
This article is protected and Monitored by DMCA
Refrences:
https://medium.com/javascript-scene/the-forgotten-history-of-oop-88d71b9b2d9f
https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/tutorials/oop