Software Training Institute in Chennai with 100% Placements – SLA Institute

Easy way to IT Job

Share on your Social Media

Object-Oriented Programming (OOP) in Python: Concepts and Techniques

Published On: April 17, 2023

Python is a flexible programming language that supports a wide range of programming techniques, including object-oriented programming (OOP) via the usage of objects and classes. The ability to create programs utilizing an Object-Oriented methodology is provided by Object-Oriented Programming, sometimes referred to as OOPs concepts in Python. It accomplishes this by grouping together behaviors and properties that are similar or related and turning them into objects. An understanding of the Python OOPs concepts are required to know more. Let’s begin with the fundamentals of the OOPs concept in Python.

Before moving on, you must be familiar with Python programming. To establish a solid base in this programming language, enroll in the best Python training in Chennai.

If we consider the car to be an object, its properties include its color, model, price, brand, and so on. Furthermore, its function would include accelerating, decelerating, and shifting gears.

Object-oriented programming: What is it?

Alan Kay, a graduate student at the time, initially coined the term “Object-Oriented Programming” (OOP), now known as OOPs concept in Python, in about 1966. In order to represent data and methods, “objects” are used in “object-oriented  programming,” a method of computer programming. Also, it is an approach to creating neat, reusable code rather than one that is superfluous. The program is broken down into multiple mini-programs or independent objects. With their own logic and data for internal communication, each individual object represents a different part of the application. An object could be something like a car. Any of the best programming languages can implement the python OOPs concept.

OOPs Concepts in Python: What are they?

Object-Oriented Programming is well-known for implementing real-world elements such as objects, hiding, inheritance, and so on in programming. Due to its strong resemblance to real-world situations, visualization is made simpler.

The key ideas of Python’s Object-Oriented Programming Language concept include class, object, method, inheritance, polymorphism, data abstraction, and encapsulation.

OOPs concept in Python: Class

A class is a group of objects that serve as the blueprint for individual objects, having common attributes and behavior. Classes are user-defined data structures, as opposed to primitive data structures. They improve the manageability of the code. It is a logical entity with a few methods and attributes.

Syntax:

class ClassName:

<statement-1>

.

.

<statement-N>

OOPs concept in Python: Object

The object is a self-contained entity with state and behavior. It could be any real-world object, such as a mouse, keyboard, chair, table, pen, and so on.

Python treats everything as an object, and most objects have attributes and functions. Every function comes with an inbuilt attribute called __doc__ that returns the docstring specified in the function’s source code.

In order to allocate memory, a class must generate an object when it is defined.

Syntax: obj = class1()

Take the example below into consideration.

Here obj refers to the “object  “of class1.

class Parrot:

# class attribute

name = “”

age = 0

# create parrot1 object

parrot1 = Parrot()

parrot1.name = “Blu”

parrot1.age = 10

# create another object parrot2

parrot2 = Parrot()

parrot2.name = “Joue”

parrot2.age = 15

# access attributes

print(f”{parrot1.name} is {parrot1.age} years old”)

print(f”{parrot2.name} is {parrot2.age} years old”)

Output

Blu is 10 years old

Woo is 15 years old

OOPs concept in Python: Inheritance

Inheritance is the most significant feature of Object-Oriented Programming (OOPs) concept in Python, which models the real-world idea of inheritance. It states that all the attributes and behaviors of the parent object are transferred to the child object.

Inheritance allows us to build a class that makes use of all the characteristics and actions of another class. The new class is referred to as a derived class or child class, whereas the class whose properties are inherited is referred to as a base class or parent class.

It allows the code to be reused.

  •         Single Inheritance:

Single level inheritance allows for the inheritance of traits from a single parent class by a derived class.

  •         Multilevel Inheritance:

By the use of multiple levels of inheritance, derived classes can have access to the properties of their parent class as well as their immediate parent class.

class employee()://Super class

def_init_(self,name,age,salary):

self.name = name

self.age = age

self.salary = salary

class childemployee1(employee)://First child class

def_init_(self,name,age,salary):

self.name = name

self.age = age

self.salary = salary

class childemployee2(childemployee1)://Second child class

def_init_(self,name,age,salary):

self.name = name

self.age = age

self.salary = salary

emp1 = employee(‘harsh’,20,3000)

emp2 = childemployee1(‘archana’,21,4500)

print(emp1.age)

print(emp2.age)

Output: 22,23

OOPs concept in Python: Encapsulation

One of the fundamental components of the Object-Oriented Programming concept in Python is encapsulation. The grouping of characteristics and methods into a single class is referred to as encapsulation.

It restricts access to and modification of a class’s attributes and methods by exterior classes. Moreover, this aids in data hiding.

class Computer:

def __init__(self):

self.__maxprice = 900

def sell(self):

print(“Selling Price: {}”.format(self.__maxprice))

def setMaxPrice(self, price):

self.__maxprice = price

c = Computer()

c.sell()

# change the price

c.__maxprice = 1000

c.sell()

# using setter function

c.setMaxPrice(1000)

c.sell()

Output

Selling Price: 900

Selling Price: 900

Selling Price: 1000

OOPs concept in Python: Polymorphism

Two terms, “poly” and “morphs,” make up the term polymorphism which is a component of OOPs concept in Python. The word poly means “many,” and the word morph means “shape.” We take polymorphism to mean that a single activity can be carried out in a variety of ways. You might have a class animal, for instance, and all animals can speak. But they communicate in various ways. In this case, the animal-specific “talk” behavior is polymorphic in a sense. Hence, while the abstract concept of “animal” does not literally “speak,” certain animals (such as dogs and cats) have a concrete implementation of the action “speak.”

class Animal:

def intro(self):

print(“There are many types of animals.”)

def speech(self):

print(“Most of the animals can speak but some cannot.”)

class dog(animal):

def speech(self):

print(“Dogs can speak.”)

class rat(animal):

def speech(self):

print(“Rats cannot speak.”)

obj_anml = Animal()

obj_dog  = dog()

obj_rat  = rat()

obj_anml.intro()

obj_anml.speech()

obj_dog.intro()

obj_dog.flight()

obj_rat.intro()

obj_rat.speech()

Output

There are many types of animals.

Most of the animals can speak but some cannot.

There are many types of animals.

Dogs can speak.

There are many types of animals.

Rats cannot speak.

OOPs concept in Python: Data Abstraction

Data Abstraction, being another important component of OOPs concept in Python shields the user from seeing unnecessary code information. Furthermore, data abstraction was created when we didn’t want to reveal delicate aspects of our code implementation. Python allows for the creation of abstract classes to achieve data abstraction.

It is impossible to instantiate an abstract class, which essentially means that you can’t make objects for this type of class. Just inheriting the functionalities may be done using it.

from abc import ABC,abstractmethod

class employee(ABC):

def emp_id(self,id,name,age,salary):    //Abstraction

pass

class childemployee1(employee):

def emp_id(self,id):

print(“emp_id is 12345”)

emp1 = childemployee1()

emp1.emp_id(id)

Output:

emp_id is 12345

In the example above, you can see that we imported an abstract method, and the remaining code has a parent class and a derived class. For the ‘childemployee’ base class, an object is created and the abstract functionality is applied.

Conclusion:

The fundamentals of Object-Oriented Programming (OOPs) concepts in Python have now been covered in this article. The object-oriented programming (OOPs) paradigm in Python is a style of programming that makes use of objects and classes. Python’s object-oriented programming (OOPs) concepts are designed to incorporate real-world notions like inheritance, polymorphism, encapsulation, etc. This OOPs concept can be implemented in other programming languages, like JAVA, etc.

Share on your Social Media

Just a minute!

If you have any questions that you did not find answers for, our counsellors are here to answer them. You can get all your queries answered before deciding to join SLA and move your career forward.

We are excited to get started with you

Give us your information and we will arange for a free call (at your convenience) with one of our counsellors. You can get all your queries answered before deciding to join SLA and move your career forward.