OOPS! Python! -By Rohit Sharma

Well, here I am with my 4th blog in "Let's talk python". Due to my exams, I was quite busy and thus couldn't upload any blog lately. Now, I am back to continue where I left. The next week we studied the major OOPS concepts. OOPS stands for Object-Oriented Programming System in programming.

OOPS revolves around making data and its behaviors (here, methods) available at a single place called an object. A class is a blueprint of that object that is how everything will be available in the object is specified by the class.

In the example, see that the class ICICI has behaviours (methods) of calculating the interest and displaying it. It is a blueprint of how everything should be in the ICICI bank’s working criterion.

OOPS has 4 major concepts in it:

a.      Inheritance

b.     Abstraction

c.      Encapsulation

d.     Polymorphism

Let’s understand each of them one by one starting with Inheritance.

What is inheritance?

In biology, inheritance is the process by which an offspring acquires characteristics of the parent[s]. The reason some of us are complimented as "You have hair like your mother", "Your eyes are on your grandma", etc is that we have inherited those traits from them.

In programming, with inheritance, a child class often known as a subclass inherits attributes and methods from its parent class often called a super-class.

In the example given below, note that we originally defined "msg(self)" in the BaseClass. As the ChildClass inherits the BaseClass it can use its attributes. So, now the ChildClass is also using msg to display a message. Have a quick glance at the syntax too.

Inheritance can be done in different combinations.

An important point to note is that multiple inheritance is possible in python.

What is abstraction?

Abstraction is normally defined as a technique that generalizes functions for users and lets them focus only on what is relevant to them, i.e. what the object does.

As a real-world example, let's take the example of a mobile phone. There are 2 buttons, green and red on the screen when somebody calls you. The common feature among them is that they both are buttons and are required to do some operation when someone calls you. Both of these buttons have different functionalities as one attends the call and the other detains it. Class call implements a method button and classes attend and detain use that button to show up in colors and respective functionalities.

Have a look at the example given down below:

For using abstraction you need to import ABC the abstraction object for the classes and "abstractmethod", the method for abstract functions.

Class shapes is an abstract class as you can see in the parenthesis, we have passed the "ABC" abstract object.

Using @abstractmethod before "def area" is the syntax to implement an abstract method.

The abstract class Shapes here has an abstract method area. And this area is modified by the classes rectangle and triangle however they want. You can see we entered the length and breadth and they gave out the output according to them.

This is what abstraction is, generalizing the common methods (here area) whose working (formula) maybe/ usually is different for each class.

What is encapsulation?

Encapsulation is hiding implementation details from the user or client. Here different tasks/ data are wrapped up in a single unit because they help us achieve a common goal.

Let's revisit the example explained in the abstraction. Now, showing up of buttons in two different colors is an abstraction. But do you know how the mobile is wired to reach these goals? Do you know what happens internally when you click on any of those buttons? Do you have any idea about the integrated circuits inside?

The answer is No!

So the developers have hidden those things from you giving you no access to modify them. That means you can't reprogram those buttons for your phone.

Let's see the example given below:

In Python, there is no word such as private so we declare them using "_" protected and "__" private. You see, the "self.__z" is declared private here and hence can never be called outside the class.

You can use setter and getter method to set this private variable to some value and then get it if you want to use it outside the class.

Note that “_y” is protected and is suggested to be used inside the class. However, this suggestion is merely a guideline and not a protocol.

What is polymorphism?

Let's use the cliché definition first to get the worldly view of polymorphism and then understand it in actuality.

Polymorphism is derived from a Greek word poly meaning many and morphs [contextual] meaning form that means an object existing in many forms. 

We shall this polymorphism using inheritance in the following code. I have achieved polymorphism using method overriding. Traditional method overloading isn't viable in python. It has to be reached differently to be executed.

See in the code below, I entered 2 types of entries one of which had address and one didn’t. The method that displays them is same so it should generate error. But, here, I have made the method overridden using inheritance features. Now, this method is available for taking both kinds of entries. 

This was all that we studied about OOPS in python. The week ended with a gist of using SQL for databases in python. This week we have started doing SQL. SQL plays a significant role in data science when dealing with databases.

As I recall, the week was as interesting as it could get. I am so happy that I am a part of this erudite program. I shall be posting the 5th blog in "Let's talk python" by the end of this week. I am so excited to know what else we can do in this amazingly complete language. 


Contact Us:

A-116, The Corenthum A-65,

Sector 62

Noida 201301

UP, India

Phone: +91 8527207966


© 2020 TheIkigaiLab  Terms of Use Privacy and Security Statement