Python Abstraction & Encapsulation
What is Abstraction?
Abstraction refers to hiding unnecessary details to focus on the whole product instead of parts of the project separately. It is a mechanism that represents the important features without including implementation details. Abstraction helps us in partitioning the program into many independent concepts so we may hide the irrelevant information in the code. It offers the greatest flexibility when using abstract data-type objects in different situations.
Example of Abstraction:
Let us take the example of a car. It has an engine, tires, windows, steering wheel, etc. All these things combine to form a car, which is an abstraction, and all the different parts are its layers of abstraction. Now an engine is composed of various parts such as camshaft, valves, oil pan, etc. these flayers the engine is an abstraction. In simple words, abstraction can be achieved by hiding the background details and showing only the necessary ones. In programming, abstraction can not be achieved without Encapsulation.
What is Encapsulation?
Encapsulation means hiding under layers. When working with classes and handling sensitive data, global access to all the variables used in the program is not secure. In Encapsulation, the internal representation of an object is generally hidden from the outside to secure the data. It improves the maintainability of an application and helps the developers to organize the code better.
Example of Encapsulation
We can take an example of a capsule in which the medicine is encapsulated. We have often used examples of bigger projects in which many programmers contribute according to their tasks. In the end, the whole project is done by joining the contribution of each participant. Well, this is what Encapsulation aims to achieve.
Abstraction and Encapsulation are fundamental concepts of OOP. Encapsulation takes all the worry away from the user, providing him with just the product that he requires, irrespective of the way it is formed. Abstraction focuses on the working of the object instead of the how part, while Encapsulation is all about hiding the way or method of working and just providing the working model.
Classes can be a perfect example of abstraction as each team member is given a separate class to work on, to develop a more significant project. A person working in a class only knows his job. While Encapsulation can be said to hide the code from normal users by making a front end through which the user can interact through the software without direct access to the code.
AbstractionAbstraction is used to solve the problem and issues that arise at the design stage.
Abstraction focuses on what the object does instead of how the details are implemented.
Abstraction can be implemented by using Interface and Abstract Class.
Its application is during the design level.
EncapsulationEncapsulation is used to solve the problem and issue that arise at the implementation stage.
Encapsulation focuses on hiding the code and data into a single unit to secure the data from the outside world.
Encapsulation can be implemented using Access Modifiers (Public, Protected, and Private.)
Its application is during the Implementation level.