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.
Abstraction
Abstraction 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.
Encapsulation
Encapsulation 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.
Example
class Employee:
no_of_leaves = 8
def __init__(self, aname, asalary, arole):
self.name = aname
self.salary = asalary
self.role = arole
def printdetails(self):
return f"The Name is {self.name}. Salary is {self.salary} and role is {self.role}"
@classmethod
def change_leaves(cls, newleaves):
cls.no_of_leaves = newleaves
@classmethod
def from_dash(cls, string):
return cls(*string.split("-"))
@staticmethod
def printgood(string):
print("This is good " + string)
harry = Employee("Harry", 255, "Instructor")
rohan = Employee("Rohan", 455, "Student")
karan = Employee.from_dash("Karan-480-Student")
Employee.printgood("Rohan")