OOP in Python Part_02

Difference between POP & OOP

differenc_oop.jpeg

Principles of Object Oriented Programming

OOPs is based on the concept of objects rather than actions, and data rather than logic. In order for a programming language to be object oriented,it should have mechanism to enable working with class & objects as well as the implementation . Usage of the fundamental object oriented principles and concepts namely Inheritance, Abstraction,Encapsulation and Polymorphism.

Abstraction

abstraction.png

Abstraction allows us to hide the details and expose only the essential features of a concept or obect . For example, a person driving a car . He knows that on pressing a horn , sounds emitted . But he has no idea about how the sound is actually generated on pressing the horn . The main focus of abstraction is to hide unnecessary details from the users. It is one of the important concepts of OOPs.

Encapsulation

encapsulation.jpg

Encapsulation is an attribute of an object & it contains all data which is hidden. That hidden data can be restricted to the members of that class. This property hides unnecessary details and makes it easier to manage the program structure .It has two divided into two part.

  1. Data hiding

  2. Bundling of data and methods together and it work with one unit.

Here levels are public , protected, private, internal and protected internal.

In other words ,Encapsulation in Python describes the concept of bundling data and methods within a single unit. So, for example, when you create a class, it means you are implementing encapsulation. A class is an example of encapsulation as it binds all the data members (instance variables) and methods into a single unit.

Inheritance

inheritance.jpg

A class is based on another class and uses data & implementation of other class .In inheritance, the child class acquires all the data members, properties, and functions from the parent class. Inheritance aslo called generalization , allows us to capture a hierachal relationship between classes and objects. For instance, a 'Fruit' is a generalization of orange . Inheritance is very useful from a code reuse perspective.

Another way , The process of inheriting the properties of the parent class into a child class is called inheritance. The existing class is called a base class or parent class and the new class is called a subclass or child class or derived class.

Polymorphism

polymorphism.png

Polymorphism means many forms .It is a thing or action is present different forms or ways. In simple words, polymorphism allows us to perform the same action in many different ways.For example, Ayisha acts as an employee when she is at the office. However, when she is at home, she acts like a wife. Also, she represents herself differently in different places. Therefore, the same person takes different forms as per the situation. In polymorphism, a method can process objects differently depending on the class type or data type.