avatarSamer Sallam

Summary

This article is an exploration of the concepts of abstraction and encapsulation within the context of Python's object-oriented programming, serving as the eighth part of a comprehensive course on the subject.

Abstract

The article delves into two foundational principles of object-oriented programming (OOP) in Python: abstraction and encapsulation. It begins by acknowledging the reader's progress in understanding Python classes, having learned to add various types of attributes and methods. The focus then shifts to encapsulation, described as the bundling of all class attributes and methods into a single unit—the class—to facilitate code maintenance and modularity. The author uses the metaphor of a capsule to emphasize how encapsulation keeps related elements together. Next, the article addresses abstraction, which involves concealing the complex implementation details from the user, allowing them to interact with the class through a simplified interface. The UBER application is cited as an example where users experience the app's functionality without needing to understand the underlying computations. The article concludes by summarizing the key points about encapsulation and abstraction, emphasizing their roles in making code easier to maintain and share. Additionally, the author encourages readers to subscribe for updates and consider Medium membership to support the writer and access more content.

Opinions

  • The author believes that understanding encapsulation and abstraction is crucial for readers who are learning OOP in Python.
  • Encapsulation is portrayed as a beneficial practice that simplifies code maintenance by logically grouping related attributes and methods.
  • Abstraction is presented as a key feature that enhances code sharing by hiding unnecessary complexity from the end-user.
  • The author expresses enthusiasm and encouragement for the reader's journey in mastering OOP, suggesting a sense of pride and accomplishment in the reader's progress.
  • The article promotes the idea that learning OOP can be likened to building a complete class in Python, with the implication that this knowledge is both practical and empowering.
  • By providing subscription and membership options, the author conveys a desire to build a community of engaged readers and to be compensated for their educational content.

Abstraction and Encapsulation: Python OOP Complete Course — Part 8

Photo by Nataliya Vaitkevich on Pexles

Before we start let me tell you that:

  • This article is a part of The Complete Course in Object-Oriented Programming in Python which you can find it here.
  • This article is also available as a YouTube video here.

Introduction

After you are armed with the all knowledge that you need to build a complete class in Python. Now, it is time to understand the meaning of the first two pillars of object-oriented programming which are Abstraction and Encapsulation. The remaining are inheritance and polymorphism which are going to be covered in future articles. Refer to Figure 1.

Figure1: Python OOP 4 Pillars (Screenshot By Author)

So this article will cover the following outlines:

  1. A quick reminder about what you have learned so far
  2. Encapsulation
  3. Abstraction

1. A quick reminder about what you have learned so far

Photo by cottonbro on Pexles

If you are reading this article right now, congratulations!! you have done a great job. Now you can build a complete class in Python in which you can add:

  • Instanced attributes and methods.
  • Class attributes and methods.
  • Static methods.
  • Properties.

You have seen that in object-oriented programming, we can create new data types by binding together attributes and the methods that process these attributes. In the context of OOP, this is known as Encapsulation.

Photo by Artem Maltsev on Unsplash

2. Encapsulation

Encapsulation is combining all the attributes and the methods that the class needs under one umbrella which is the class itself.

Simply imagine your class just like a capsule and inside this capsule, you have together all the attributes and the methods that process these attributes. Refer to figure 2.

Figure 2: class like a capsule (Image By Author)

Now, you can combine everything in one place and because of that encapsulation, which is the first pillar of OOP, makes your code easy to be maintained because you are keeping everything related to one class under one umbrella.

Photo by Ricardo Resende on Unsplash

So, if any problem appears related to this class, directly you will refer to the file in which you have this class, and you will look for the problem. This will reduce seriously the amount of time that you need to maintain your code.

Next, let us talk about the second pillar of OOP which is Abstraction.

3. Abstraction

Abstraction is hiding away the implementation details from the external user. Refer to Figure 3.

Figure 3: Abstraction description (Image By Author).

In other words, imagine that your class is like a toolbox and there are many tools within this toolbox. If you want to share your class with other people, no need to share all these tools with them however you can tell them about one or two tools, the main tools that depend on the other tools, and the external user cares only about this one or two tools.

For example UBER application:

https://www.bbc.com/news/business-47871117

When you want to use the UBER application to get a taxi for your trip. Simply, you add your location and the destination location. After that, the application will calculate the best route for you and the estimation time, etc., etc... As a result, all of these details of calculation and estimation are away from you as an external user and the application will do everything for you behind the scene.

So, abstraction makes your code easy to be shared because when you want to share your code with other people, you have to tell them only about the tools that they care about.

Briefly, people will care about how to use the class, not about the actual implementation of your class.

Great, now if you hear Encapsulation and Abstraction terms in any discussion you know exactly what they mean

Now, let us summarize what we have learned in this article:

Photo by Ann H on pexels

In this article you have learned about:

  • Encapsulation: combining all the attributes and the methods that the class needs under one umbrella (class itself) makes your code easy to maintain.
  • Abstraction: hiding away the implementation details from the external user which makes your code easy to be shared with other developers.

P.S.: A million thanks for your time reading my story. Before you leave let me mention quickly two points:

  • First, to get my posts in your inbox directly, would you please subscribe here, and you can follow me here.
  • Second, writers made thousands of $$ on Medium. To get unlimited access to Medium stories and start earning, sign up now for Medium membership which only costs $5 per month. By signing up with this link, you can directly support me at no extra cost to you.

To get back to the previous article, you can use the following link:

Part 7:Property in Python

To move on to the next article, you can use the following link:

Part 9:Shallow Copy and Deep Copy

Object Oriented
Python
Programming
Abstraction
Encapsulation
Recommended from ReadMedium