Code in Python
Machine Learning and Deep Learning Blogs
search

Object Oriented Programming in Python

Python supports object-oriented programming. For those of you who already have a background in another programming language, the concept of object-oriented programming in Python is similar. For our programming beginners, this article will explain the concepts of object oriented programming.

Earlier before the object-oriented programming was introduced, programming languages used to follow procedural programming paradigm. A paradigm is a pattern or a model.

Procedural programming is based on the way the particular program is coded. The focus is not on the data but the way they are implemented. Real world objects become difficult to relate in procedural paradigm. It uses functions and variables to code behavior and attributes of real world objects. To solve this problem object oriented programming is introduced. Here the focus is majorly on the objects, their behavior, and attributes.

Object oriented programming has following features which make real world modeling easy compared to procedural programming.

  1.  Encapsulation
  2.  Abstraction or Data Hiding
  3.  Polymorphism
  4.  Inheritance

Object oriented programming is implemented using class. A class is a user-defined data type in which we can enclose variables and functions. To access these functions and variables we use objects.

Encapsulation

Wrapping of code and data together into a single unit is known as encapsulation. Encapsulation is what helps to construct class by placing data and the functions that work on that data in the same place. Object-oriented programming provides you the framework to place the data and the relevant functions together in the same object.

Abstraction or Data Hiding

Data abstraction refers to the process of providing only the essential information to the outside world or users and hiding their background details. Data Abstraction or data hiding will only represent the important information in the program that user needs to know without presenting the details behind it.

Polymorphism

The ability to use an operator or function in different ways by giving them different meaning or functionality to the operators or functions is called polymorphism. Poly refers to many. The function name or operator remains the same. They differ by a number of arguments or type of arguments while defining them.

For example: An operator “+” can be used to add 2 numbers in python. But we are also allowed to add 2 strings or concatenate two strings using “+” operator. This is due to a polymorphism that “+” has an additional function to concatenate strings.

Inheritance

Inheritance is the process of forming a new class from an existing class. The existing class is called the base class and the new class formed is called the derived class. The derived class has access to all the data members and functions since it is inheriting those from the base class. So in derived class, we have to add new functionalities or override the existing one derived from the base class. One of the most important aspects of object-oriented programming is code reusability and inheritance successfully helps to achieve so and reduces code size.

For example: We have a class Animal which has attributes like size, color, voice and functions like hasOwner(), hasName(), getDetails(), etc. Now if we define a class Dog to create new Dog objects then instead of writing all the functionality separately for Dog class we can inherit them from Animal Class. We can add our own attributes in Dog class like breed, height, etc. apart from those inherited.

Akarsh Singh
Follow me

Akarsh Singh

Founder, Code in Python. Web and Android App Developer. He loves Python and wants to share his knowledge via this website. Follow him on Facebook, Twitter and Github.
Akarsh Singh
Follow me

Latest posts by Akarsh Singh (see all)

4 Comments

  1. Thank you for the auspicious writeup. It in fact was a enjoyment account it. Glance complicated to more delivered agreeable from you! By the way, how can we keep up a correspondence?

  2. I would like to thank you for the efforts you have put in writing this site. I’m hoping to view the same high-grade blog posts by you in the future as well. In fact, your creative writing abilities has encouraged me to get my very own blog now 😉

  3. Undeniably believe that which you said. Your favorite reason appeared to be on the net the simplest thing to be aware of. I say to you, I certainly get annoyed while people think about worries that they plainly don’t know about. You managed to hit the nail upon the top as well as defined out the whole thing without having side-effects , people could take a signal. Will probably be back to get more. Thanks

  4. Hello, the whole thing is going fine here and ofcourse every one is sharing facts, that’s truly good, keep up writing.

Leave a Reply

Your email address will not be published.

*

© 2018 Code in Python