Object Oriented Programming in Python
Python supports different programming approach where the Object Oriented approach is also one. In this approach, the solution to the problem is found out by creating new objects. Objects in Python represents the real world things like Car, Chair, Apple, etc.
Object-Oriented Programming uses various techniques to provide us with various functionalities that help to write an efficient program. Let us go through the terminology used for Object Oriented Programming in Python programming.
Class
A class is a logical entity that contains attributes and methods that defines the object. The syntax of defining a class is
class ClassName:
.
.
<statement-N>
The statements in the class consist of attributes that define the characteristics of the object and methods that operate on these attributes.
Object
An object is the unique instance of the class where the real operations are done. Since class only encapsulates the data, the object is the one that holds those data.
Inheritance
OOP provides an inheritance feature that helps to create child objects that can acquire the attributes and the methods of the parent classes. The parent class is called base class and the child class is called derived class. It provides us with code reusability.
Polymorphism
OOP provides an inheritance feature that helps to create child objects that can acquire the attributes and the methods of the parent classes. The parent class is called base class and the child class is called derived class.
Encapsulation
The prevention of data being modified directly by external objects is known as encapsulation. It restricts the access to methods and attributes (variables) of the class.
In Python, we denote private attribute using underscore as prefix i.e single “ _ “ or double “ __“.
Polymorphism
Polymorphism means using the same name for different functionality. We can have two classes that have some common method, but they have different functionality based on the class type. For example, there may be two classes for representing birds ( Crow and Penguin), where Crow can fly but Penguin cannot. So, we can use fly() as a method in both classes but they might serve a different purpose.
Data Abstraction
OOP provides data abstraction feature to hide internal details. It is used to secure the data in the class.
Object-oriented vs Procedure-oriented Programming languages
Index | Object-oriented Programming | Procedural Programming |
---|---|---|
1. | Object-oriented programming is the problem-solving approach where real world things are represented by objects. | Procedural programming uses lists of instructions that are carried out in a step by step. |
2. | It makes development and maintenance easier. | The development is easier in procedural, but the maintainability of the code is not easier compared to object-oriented programming. |
3. | Real-world problems can be effectively solved through the object-oriented approach. | It doesn’t simulate the real world. It works on step by step instructions divided into small parts called functions. |
4. | It provides data hiding. So it is more secure than procedural languages. You cannot access private data from anywhere. | Procedural language doesn’t provide any proper way for data binding, so it is less secure. |
5. | Example of object-oriented programming languages is C++, Python, Java, .Net, C#, etc. | Example of procedural languages are: C, Fortran, Pascal, VB etc. |