Python: Object-Oriented Programming Language [OOPs]

Python OOP Concepts

In this article, we will explore the Python OOP (Object-Oriented Programming) concepts. OOP stands for Object-Oriented Programming, and it is defined by various concepts such as objects, classes, methods, etc.

Another type of programming paradigm is Procedural-Oriented Programming (POP). You may have heard that many experts began their careers with the C language, which is a Procedural-Oriented Programming language.

There are many limitations and complexities with POP, which led to the development of a new concept—Object-Oriented Programming (OOP). OOP languages are very popular and are used in almost all major modern programming languages. They are also being considered for new languages currently under development.

The primary motivation for developing OOP languages was to manage the increasing size and complexity of programs.

Now, the question is, why are OOP languages so popular, and why do so many people want to learn these concepts? You’ll discover everything you need to know about OOP features in this article—stay tuned!

Python is an Object-Oriented Programming language, and here we will discuss OOP concepts using simple Python programs as examples. Don’t worry if you are unfamiliar with Python. It’s not necessary to know Python in advance to understand OOP concepts in general or OOP in Python.

PYTHON OOP: Basic Concepts

Any complex language starts with the simple concept irrespective of complexities involved in that areas. There are few simple python OOP features but it is base of python programming models.

Python OOP Features:

  • Class
  • Object
  • Abstraction
  • Polymorphism
  • Inheritance
  • Encapsulation

Class

In simple words, class is a grouping of data and its associated functions. Once a class has been defined, we can create any number of objects associated with that class.

For example, Account_Number, Balance, Credit() and Debit() are the member of Class Bank.

If Bank has defined as a class, then the statement Bank-Balance , will create an object Balance belonging to the class Bank.

Examples:

Fruit SchoolBank Computer 
MangoRoll_NumberAccount_NumberHDD
AppleStudent_NameBalanceRAM
OrangeStudy()Credit()Processors
 Exam()Debit()Processing()

Object

Object is an instance of a class. In other words, Object is a way to access features of a class. As described above a class contains the associated data and its function but to access its features Objects play major role.

Consider class – Bank from above example and try to understand the power of object.

You already have defined class- Bank with associated data and functions to perform credit and debit operations.

e.g. The normal operation happening in bank system is account holder debit some money from account or credit some money into account.

Assume Customer-1 has defined cust1 as an object of class Bank and Customer-2 has defined cust2 as an object of same class Bank.

Now we try to credit some amount into customer 1’s account and debit some amount from customer 2’s account by accessing features of Class Bank through objects cust1 and cust2 respectively.

				# Credit 1000 in customer-1 account
cust1.credit(1000)

# Debit 500 from customer-2 account
cust2.debit(500)
			

Abstraction

An abstraction is very important aspect to hide complexity or internal background processes and represent essential features.

To understand this concept more clearly, take the same example of Class Bank.

In first two points, we have already seen credit and debit example while describing class and object.

Observe those examples, you are just calling function by pointing out correct class object with value. In this process you need not to know what is happening inside when you are calling credit or debit functions.

What & How process happens during these transactions are hidden to end users.

This is an Abstraction.

Abstraction, where you only know the essential things to operate on banking system without knowing the background details of actual complex calculations.

It gives flexibility and simplicity to the users to use complex application knowing minimal required things to operate on.

Polymorphism

A single function name used to handle multiple operations by passing necessary arguments, that is called as Polymorphism. This is something similar to a particular word having several different meanings depending upon the context.

For example,
You have a class shape, and you want to draw multiple shapes but they draw differently.

Here, the “draw” behavior is polymorphic in the sense and depends on the shape. So, the abstract “shape” concept does not actually “draw”, but specific shapes (like circle or rectangle) have a concrete implementation of the action “draw”.

Pro Tip: Function overloading is not supported in Python OOP.

Polymorphism plays an important role in allowing objects having different internal structures to share the same external interface.

Polymorphism is extensively used in implementing inheritance.

Inheritance

Inheritance is the process of  creating new class based on an existing class.

In other words, it is the proces by which objects of one class acquired the properties of objects of another classes. It supports the concept of hierarchical classification.

The one whose properties are acquired is known as a base class or parent class and the new class is known as a derived class or child class.

For example,

Inheritance can be easily described with class computer. In the computer class we have defined all its accessories and features. Another class created for – Laptop by inheriting its properties from class computer.

In this case, Computer is the base or parent class and Laptop is the derived or child class.

Encapsulation

Encapsulation is also an important feature of object oriented programming language.

The wrapping up of data and function into a single unit (called class) known as encapsulation. If you are creating class, you are doing encapsulation.

The data abstraction and encapsulation go hand-in-hand. An abstraction is used to hide internal details and show only functionalities, it can be only achieved through encapsulation.