Course Content

Abstraction & Encapsulation | Python Tutorials For Absolute Beginners In Hindi #59

Our today's tutorial is based mainly on theory because coding is not just about learning new concepts but also about understanding them. Let us give our PyCharm some rest and try to learn some theory about Abstraction and Encapsulation, which are very important concepts for our tutorials ahead.

What is Abstraction?

Abstraction refers to hiding the unnecessary details so that the focus could be on the whole product instead of parts of the project separately. It is a mechanism that represents the important features without including implementation detailsAbstraction helps us in partitioning the program into many independent concepts so, we may hide the irrelevant details 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 forming a car, which is an abstraction, and all the different parts are its layers of abstraction. Now an engine is composed of different 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 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 the task assigned to them. 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. What Encapsulation does is it takes all the worry away from the user, providing him with just the product that he required, 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 member of a team is given a separate class to work on, to develop a bigger project. A person working in a class only knows his job. While Encapsulation can be said as hiding the code from the normal users by making a front end through which the user can interact through the software, without having any direct access to the code.

Abstraction

Encapsulation

Abstraction is used to solves the problem and issues that arise at the design stage.

Encapsulation is used to solves the problem and issue that arise at the implementation stage.

Abstraction focuses on what the object does instead of how the details are implemented.

Encapsulation focuses on hiding the code and data into a single unit to secure the data from the outside world.

 Abstraction can be implemented by using Interface and Abstract Class.

Encapsulation can be implemented using Access Modifiers (Public, Protected, and Private.)

  Its application is during the design level.

Its application is during the Implementation level.

 This tutorial is all about understanding the concept of Abstraction and Encapsulation. Check the next tutorials to learn how to implement more OOP concepts in Python.

Code as described/written in the video


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")


Be the first person to comment!

Comments(0)

Resources

No downloadable resources for this video. If you think you need anything, please post it in the QnA!

Course Announcements

Any Course related announcements will be posted here