What is OOP’s Concept In Python?

Published by Anshul Verma on

  1. Objected oriented programming as a discipline has gained a universal following among developers. Python, and in-demand programming language also follows an object-oriented programming paradigm. It deals with declaring Python classes and objects which lays the foundation of OOPs concepts. This article on “object oriented programming python” will walk you through declaring python classes, instantiating objects from them along with the four methodologies of OOPs.

In this article, following aspects will be covered in detail:

  • What is Object Oriented Programming ?
  • Difference between object and procedural oriented programming
  • What are Python OOP’s Concepts ?
  • Object-Oriented Programming methodologies:
      • Inheritance
      • Polymorphism
      • Encapsulation
      • Abstraction

What is Object-Oriented Programming ?

Object Oriented Programming is a way of computer programming using the idea of “objects” to represents data and methods. It is also, an approach used for creating neat and reusable code instead of a redundant one. the program is divided into self-contained objects or several mini-programs. Every Individual object represents a different part of the application having its own logic and data to communicate within themselves.

Now, to get a more clear picture of why we use oops instead of pop, I have listed down the differences below.

Difference between Object-Oriented and Procedural Oriented Programming

Object-Oriented Programming (OOP) Procedural-Oriented Programming (Pop)
It is a bottom-up approach It is a top-down approach
Program is divided into objects Program is divided into functions
Makes use of Access modifiers

‘public’, private’, protected’

Doesn’t use Access modifiers
It is more secure It is less secure
Object can move freely within member functions Data can move freely from function to function within programs
It supports inheritance It does not support inheritance

What are Python OOPs Concepts ?

Major OOP (object-oriented programming) concepts in Python include Class, Object, Method, Inheritance, Polymorphism, Data Abstraction, and Encapsulation.

Refer the previous blog for Class & Object.

Object-Oriented Programming methodologies :

Object-Oriented Programming methodologies deal with the following concepts.

  • Inheritance
  • Polymorphism
  • Encapsulation
  • Abstraction

Let us understand the first concept of inheritance in detail.

Inheritance :

Ever heard of this dialogue from relatives “you look exactly like your father/mother” the reason behind this is called ‘inheritance’. From the Programming aspect, It generally means “inheriting or transfer of characteristics from parent to child class without any modification”. The new class is called the derived/child class and the one from which it is derived is called a parent/base class.

TypesOfInheritance - Python class - Edureka

1. Single Inheritance :

Single level inheritance enables a derived class to inherit characteristics from a single parent class.

Example:

Explanation:

  •  I am taking the parent class and created a constructor (__init__),  class itself is initializing the attributes with parameters(‘name’, ‘age’ and ‘salary’).
  • Created a child class ‘childemployee’ which is inheriting the properties from a parent class and finally instantiated objects ’emp1′ and ’emp2′ against the parameters.
  • Finally, I have printed the age of emp1. Well, you can do a hell lot of things like print the whole dictionary or name or salary.

2. Multilevel Inheritance :

Multi-level inheritance enables a derived class to inherit properties from an immediate parent class which in turn inherits properties from his parent class.

Example:

Explanation:

  • It is clearly explained in the code written above, Here I have defined the superclass as employee and child class as childemployee1. Now, childemployee1 acts as a parent for childemployee2.
  • I have instantiated two objects ’emp1′ and ’emp2′ where I am  passing the parameters “name”, “age”, “salary” for emp1 from superclass  “employee” and “name”, “age, “salary” and “id” from the parent class “childemployee1”

 

3. Hierarchical Inheritance :

Hierarchical level inheritance enables more than one derived class to inherit properties from a parent class.

Example:

Explanation:

  • In the above example, you can clearly see there are two child class “childemployee1” and “childemployee2”. They are inheriting functionalities from a common parent class that is “employee”.
  • Objects ’emp1′ and ’emp2′ are instantiated against the parameters ‘name’, ‘age’, ‘salary’.

4. Multiple Inheritance :

Multiple level inheritance enables one derived class to inherit properties from more than one base class.

Example:

Explanation : In the above example, I have taken two parent class “employee1” and “employee2” and a child class “childemployee”, which is inheriting both parent class by instantiating the objects ’emp1′ and ’emp2′ against the parameters of parent classes.

Polymorphism :

You all must have used GPS for navigating the route, Isn’t it amazing how many different routes you come across for the same destination depending on the traffic, from a programming point of view this is called ‘polymorphism’. It is one such OOP methodology where one task can be performed in several different ways. To put it in simple words, it is a property of an object which allows it to take multiple forms.

Inheritance and Polymorphism in Python - My Blog

Polymorphism is of two types:

  • Compile-time Polymorphism
  • Run-time Polymorphism

1. Compile-time Polymorphism:

A compile-time polymorphism also called as static polymorphism which gets resolved during the compilation time of the program. One common example is “method overloading”. Let me show you a quick example of the same.

Example:

Output :

Explanation:

  • In the above Program, I have created two classes ’employee1′ and ’employee2′ and created functions for both ‘name’, ‘salary’ and  ‘age’ and printed the value of the same without taking it from the user.
  • Now, welcome to the main part where I have created a function with ‘obj’ as the parameter and calling all the three functions i.e. ‘name’, ‘age’ and ‘salary’.
  • Later, instantiated objects emp_1 and emp_2 against the two classes and simply called the function. Such type is called method overloading which allows a class to have more than one method under the same name.

2. Run-time Polymorphism :

A run-time Polymorphism is also, called as dynamic polymorphism where it gets resolved into the run time. One common example of Run-time polymorphism is “method overriding”. Let me show you through an example for a better understanding.

Example:

Explanation : In the above example, I have created two classes ‘childemployee1’ and ‘childemployee2’ which are derived from the same base class ‘employee’. Here’s the catch one did not receive money whereas the other one gets. Now the real question is how did this happen? Well, here if you look closely I created an empty function and used Pass ( a statement which is used when you do not want to execute any command or code). Now, Under the two derived classes, I used the same empty function and made use of the print statement as ‘no money’ and ‘has money’. Lastly, created two objects and called the function.

Encapsulation :

encapsulation in python

In a raw form, encapsulation basically means binding up of data in a single class. Python does not have any private keyword, unlike Java. A class shouldn’t be directly accessed but be prefixed in an underscore.

Let me show you an example for a better understanding.

Example:

Explanation : You will get this question what is the underscore and error? Well, python class treats the private variables as (__salary) which cannot be accessed directly.

So, I have made use of the setter method which provides indirect access to them in my next example.

Example:

Explanation:  Making Use of the setter method provides indirect access to the private class method. Here I have defined a class employee and used a (__maxearn) which is the setter method used here to store the maximum earning of the employee, and a setter function setmaxearn() which is taking price as the parameter.

This is a clear example of encapsulation where we are restricting the access to private class method and then use the setter method to grant access.

Abstraction :

The Cost Of Abstraction In Python | PERPETUAL ENIGMA

Suppose you booked a movie ticket from bookmyshow using net banking or any other process. You don’t know the procedure of how the pin is generated or how the verification is done. This is called ‘abstraction’ from the programming aspect, it basically means you only show the implementation details of a particular process and hide the details from the user. It is used to simplify complex problems by modeling classes appropriate to the problem.

An abstract class cannot be instantiated which simply means you cannot create objects for this type of class. It can only be used for inheriting the functionalities.

Example:

Explanation:  As you can see in the above example, we have imported an abstract method and the rest of the program has a parent and a derived class. An object is instantiated for the ‘child employee’ base class and functionality of abstract is being used.

Is Python 100 per cent object-oriented?

Python doesn’t have access specifiers like “private” as in java. It supports most of the terms associated with “objected-oriented” programming language except strong encapsulation. Hence it is not fully object-oriented.

This brings us to the end of our article on “Object-Oriented Programming Python”. I hope you have cleared with all the object-oriented concepts in python. Make sure you practice as much as possible and revert your experience.

Till then, Stay Safe, Stay Happy & Keep Coding…

IF YOU WANT TO KNOW ABOUt DATA SCIENCE click here.

CONNECT WITH THE AUTHOR ON LINKEDIN.

VISIT OUR WEBSITE BRIGHTERBEES FOR MORE INTERESTING STUFFS ABOUT LEARNING.


0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

STAY CONNECT WITH US