Getting Started with Python OOPs Concepts

Getting Started with Python OOPs Concepts

24 July 2023 0 By Anshul Pal


Like other programming languages, Python is also an object oriented programming language. In this tutorial you will learn about the basics of an object-oriented programming language in Python, which is pronounced as Python OOPs Concepts. Python allows us to build applications using an object-oriented programming approach. With the help of this, we can easily create  classes and objects. OOPs is a technique to solve problems by creating classes and objects.

Concepts of Object Oriented Programming 

    • Classes
    • Objects
    • Inheritance
    • Polymorphism
    • Data Abstraction
    • Encapsulation

Python Classes

A class is a collection of objects. Class is a root from which the object is created. A class is a logical entity that includes some specific attributes and methods. Class can be created by using class keyword. To understand classes in a better way, we take an example below.

Example

If you are manager at XYZ Pvt Ltd and you want to track the number of employees who have their different attributes, like name and job role. If a list is used, the first element could be the employee’s name while the second element could represent its job role. Let’s suppose there are 500 different employees, then how would you know which element is supposed to be which? What if you wanted to add other properties to these employees? This lacks organization and it’s the exact need for classes.

Syntax

class ClassName:
    # <statement start>
         .
         .
     # <statement end>

Creating a class in Python

class APalgorithm:
    x = "Welcome to our Python OOPs Tutorial"
print(APalgorithm)

Output

<class '__main__.APalgorithm'>

Python Objects

Everything we see in python is an object and almost everything has attributes and methods. If you create a class, then elements of this class are known as objects. For example, integers, strings, floating point numbers, arrays and dictionaries or any real word objects.

Objects Consists of:

  • Identity : An identity gives a unique name to an object which helps one object to interact with others. It can be considered as the class name.
  • State : State is represented by the attributes of an object.
  • Behavior: It is represented by the methods of an object.

Example

class Person:
  def __init__(self, name, gender):
    self.name = name
    self.gender = gender

p1 = Person("Abbroy", "Male")

print(p1.name)
print(p1.gender)
Output
Abbroy
Male

In the above example, first we take a class named as Person and then we take two objects. One is name and the second one is gender and here we are using a function that is __init__. We discuss that function later in detail. Here you can only assume that it is only a function that parses our value and here you see a self- name variable which is used to parse our value into a function. It is not mandatory that you can use self variable only. You can take a different name by your choice. After that, we store our class and object values into a variable named p1 and then we print both.

The __init__() Function

This is a bulit-in function in python it is similar to constructor in C++ and Java. We use this function to assign values to object properties, or other operations that are necessary to do when the object is being created.

Inheritance

In Python, Inheritance is a fundamental object-oriented programming (OOP) concept that permit a class to inherit properties and behaviors from another class.The main class is known as Parent class which provides the properties and the sub class which are using the properties of parent class are known as the child class. Inheritance forms an relationship between the child class and parent classes, where the child class is a specialized version of the parent class, inheriting its attributes and methods. In Python, to create a child class that inherits from a parent class, you define the child class with the name followed by the name of the parent class in parentheses.

Syntax –

class ParentClass:
# Parent class attributes and methods
class ChildClass(ParentClass):
# Child class attributes and methods

There are four types of inheritance are as followed –

  • Single inheritance
  • Multilevel inheritance
  • Hierarchical inheritance
  • Multiple inheritance

Polymorphism

Python allows objects of different classes to be treated as objects of a common superclass, enabling code to work with objects in a more abstract and flexible manner. Polymorphism allows multiple classes to implement the same method name, but each class can have its own implementation of that method. When a method is called on an object, Python will dynamically determine which class’s implementation to invoke based on the actual type of the object at runtime.

There are two main types of polymorphism in Python:

  1. Compile-time Polymorphism (Method Overloading)
  2. Run-time Polymorphism (Method Overriding)

Example

class Animal:
def make_sound(self):
return "Some generic sound"

class Dog(Animal):
def make_sound(self):
return "Woof!"

class Cat(Animal):
def make_sound(self):
return "Meow!"

def animal_sound(animal):
return animal.make_sound()

# Creating instances of the child classes
dog_instance = Dog()
cat_instance = Cat()

# Using the common function with different objects
print(animal_sound(dog_instance)) # Output: Woof!
print(animal_sound(cat_instance)) # Output: Meow!

In this example, the Animal class has a method called make_sound(). The Dog and Cat classes override this method with their own implementations. When we call the animal_sound() function with instances of the child classes, Python dynamically determines which version of the make_sound() method to execute based on the actual type of the object passed at runtime, demonstrating run-time polymorphism.

Suggested Readings!

featured image source – pexels