In this article we will go through the Python OOPs Concept. OOP is termed as Object Oriented Programming and it is distinguished based on various concepts such as objects, class and methods etc.
There is one more type of language exists called as Procedural Oriented Programing language (POP). You probably heard that most of the expert started their career with C- language and this is a procedural Oriented Programming.
There are many limitations and complexity with POP hence the new concept originated – Object Oriented Programming language(OOP) with its various useful features. OOP languages are very famous and used in almost all existing popular languages, also it is getting considered for new languages which are currently under development.
The major need for developing such languages was to manage the ever-increasing size and complexity of programs.
Now question is, why OOP languages are so popular and why everybody liked to acquire those concepts. You will get to know in-and-out about OOPs features in this article – stay tuned!!
Python is an Object-Oriented Programming Language and here we will discuss OOPs concept by taking reference of simple python programs. Don’t worry, if you do not know Python. It is not necessary to know python in advance to understand OOPs concept in general or OPPs with Python.
PYTHON OOPs: 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:
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.
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.
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
# Debit 500 from customer-2 account
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.
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.
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”.
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.
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.
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.
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.