Object Oriented Programming (OOPs) in Python

The Python programming paradigm known as object-oriented programming (OOPs) makes use of objects and classes. It aims to incorporate in programming real-world concepts like inheritance, polymorphism, encapsulation, etc. The fundamental idea behind OOPs is to unite the data and the functions that use it so that no other part of the code can access it.

Object-oriented programming (OOPs) fundamental ideas include:

  • Class
  • Objects
  • Polymorphism
  • Encapsulation
  • Data Abstraction
  • Inheritance
  • Data Abstraction

Class

A class is a group of related objects. The models or prototypes used to create objects are contained in classes. It is a logical entity with a few methods and attributes.

Consider the following scenario to better understand the need for creating classes: Suppose you wanted to keep track of the number of dogs that might have various characteristics, such as breed or age. If a list is used, the dog’s breed and age could be the first and second elements, respectively. What if there were 100 different breeds of dogs? How would you know which element should go where? What if you wanted to give these dogs additional traits? This is disorganised and exactly what classes need.

Objects

The object is an entity that is connected to a state and behaviour. Any physical object, such as a mouse, keyboard, chair, table, pen, etc., may be used. Arrays, dictionaries, strings, floating-point numbers, and even integers are all examples of objects. Any single string or integer, more specifically, is an object. A list is an object that can hold other objects, the number 12 is an object, the string “Hello, world” is an object, and so on. You may not even be aware of the fact that you have been using objects.

An object includes:

  • State: An object’s attributes serve as a representation of it. Additionally, it reflects an object’s characteristics.
  • The methods of an object serve as a representation of behaviour. It also shows how an object reacts to other objects.
  • Identity: It gives an object a special name and makes it possible for one object to communicate with another.
  • Let’s look at the example of the class dog to better understand the state, behaviour, and identity (explained above).

The identity may be regarded as the dog’s name.
Breed, age, and colour of the dog are examples of states or attributes.
You can infer from the behaviour whether the dog is eating or sleeping.

Inheritance

The ability of one class to derive or inherit properties from another class is known as inheritance. The class from which the properties are being derived is referred to as the base class or parent class, and the class from which the properties are being derived is referred to as the derived class or child class. The advantages of inheritance include:

It accurately depicts relationships in the real world.
It offers a code’s reusability. We don’t need to keep writing the same code. Additionally, it enables us to expand a class’s features without changing it.
Because of its transitive nature, if a class B inherits from a class A, then all of class B’s subclasses will also automatically inherit from class A.

Different Inheritances –

Single Inheritance:

A class can inherit properties from a single-parent class using single-level inheritance.

Multilevel Inheritance:

A derived class can inherit properties from an immediate parent class, which in turn can inherit properties from his parent class, thanks to multi-level inheritance.

Hierarchical Inheritance:

More than one derived class can inherit properties from a parent class thanks to hierarchical level inheritance.

Multiple Inheritance:

One derived class may inherit properties from several different base classes thanks to multiple level inheritance.

Polymorphism

Simply put, polymorphism means having multiple forms. For instance, using polymorphism, we can answer the question of whether the given species of birds fly or not with just one function.

Encapsulation

One of the core ideas in object-oriented programming is encapsulation (OOP). It explains the concept of data wrapping and the techniques that operate on data as a single unit. This restricts direct access to variables and methods and can avoid data modification by accident. A variable can only be altered by an object’s method in order to prevent accidental modification. These variables fall under the category of private variables.

A class, which encapsulates all the data that is contained in its member functions, variables, etc., is an example of encapsulation.

Read more about OOPs

Data Abstraction

It shields the user from seeing extraneous code details. Additionally, data abstraction was created when we didn’t want to share private or sensitive portions of our code implementation.

Python allows for the creation of abstract classes to achieve data abstraction.

If you find any errors or would like to add more information to the discussion above, kindly leave a comment.