Sitemap

OOP’s Concepts using Python..

3 min readMay 30, 2020

--

In this article, I will try to explain the Object-Oriented programming concept using Python. Let’s start by asking a simple question to ourselves, what is the purpose of OOP? OOP allows us to keep our code clean, organized, and not repeatable. OOP’s help your code to be in a structured way so that commonly repeatable attributes and functions can be be made object-oriented way and more usable. We will try to discuss what is a class, object, and inheritance, and data hiding.
Let’s start with an example of a class :

Example of class

In this class is nothing but a blueprint of an object. The class will be declared only when an object will be declared, so unless it is declared no memory is allocated to it. From this example, we can see a class name sample has tow methods, init, and func. Here init is a special method called constructor defined with underscores which are used to create an instance of an object. This init method has two parameters passed to it, Self: Self keyword here is act as a reference to object which is similar to this in C++ standards, and the other one is input passed along when the object is created.

Get Rishi Krishnan Natarajan’s stories in your inbox

Join Medium for free to get updates from this writer.

The above example shows how to access the attributes and methods of a class using a reference object. Now let’s see about inheritance.
Inheritance is nothing but inheriting the attributes and methods of the base (parent) class without changing it. Inheritance helps to reuse the code and by that way, it reduces the complexity of programming

Press enter or click to view image in full size

In this example, we are inheriting the method from the base class to the child class, which avoids here the repeatability of methods and attributes. In python, we can make a variable private using __ in front of the variable, in this way if you try to access it outside the class using an object it will throw an error as it is private to the class.
Now let’s see about polymorphism:
Polymorphism is a fairly important concept in python, it allows different classes to have the same method name with different functions. It also supports method overriding by overriding the function in the base class. Let’s see it an example:

Press enter or click to view image in full size
Polymorphism

We will understand the significance of super keyword in python in a new article.

--

--