Object Oriented Concepts: Abstraction

To drive a car you need not know the internal architecture of the car or understand How the engine work, Similarly to operate a remote control you need not know the internal circuitry, all you really do it navigate from one channel to another.

Our day to day lives have become so simple as we need not know all these underlying information about the systems that we use. This is basically abstracting away complex information from users.

Abstarction is hiding the underlying details of a system

Why do we need abstraction?

If we don’t have abstraction in place, observer or the user will need to deal with entire information of the system which will make the usage of the system very complex.

Here is a example of an email service. For the client to access email service he/she will have to use internal services like connecting to server and authentication and later once email is sent disconnecting from the server. This examples shows how to abstract unwanted information from the client.

""
A class for sending Send Email
 Methods:
    1. Connect to server
    2. Authenticate request
    3. Send Mail
    4. Disconnect from server

"""

class Email:
    # __ makes the method private, these methods will not
    # be accessible in the main.

    def __connect(self):
        print("Connected to server")

    def __authenticate(self):
        print("User authenticated")

    def send_mail(self):
        self.__connect()
        self.__authenticate()
        print("Sent mail")
        self.__disconnect()

    def __disconnect(self):
        print("Disconnected")


if __name__ =="__main__":
    obj = Email()
    obj.send_mail()

# client can access only send_mail(), rest information is abstracted from 
# the client


Abstraction can be done by making the class attributes and methods as private so that it cannot we access outside the class. Following is a video for implementation of abstraction.

Hope this was helpful stay tuned for more !

Stay Safe.

Rating: 3.5 out of 5.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s