In this article we will go through the Python OOP Concepts. 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 OOP : Basic Concept
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 way, 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.
|Class Name||Function and/ Variable|
|Bank||Account_Number, Balance, Credit() and Debit()|
|Fruit||Mango, Apple, Orange, Colour()|
|School||Roll_Number, student_Name, Study(), Exam()|
|Computer||HDD, RAM, Processors, Processing()|
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. 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.
Let’s 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.
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. How and What process happening inside is hidden from you.
It gives flexibility and simplicity to the users to use complex application knowing minimal required things to operate on.
In simple words, A single function name can be used to handle different operations by passing necessary arguments. 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 have to draw the 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.
Inheritance is the process of creating new class based on an existing class. In other words, it is the process 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.
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.