Oops concepts in python with examples

Object-oriented programming language is all about creating an object where an object is a group of interrelated variables and functions. For example, a bike can be an object so if we consider it is an object then its colour, mileage and wheel size would be its properties. like other aspects of programming language object-oriented concept is necessary to learn to use them efficiently. In this article, you will learn some basic terminology of object-oriented programming language in detail.

Python built-in object

python provides us with many built-in objects which are used for different purposes, for example, appending data in a list and removing data from a list. Here are a few lines of code that illustrates how the object is used multiple time in a single program.

car=list()
car.append("Tesla")
car.append("BMW")
print(car)
car.sort()
print(car)
Output:
['Tesla', 'BMW']
['BMW', 'Tesla']

In the above program, the first line, where the object of the type list is created and then the append method, is called for adding data in a list.

Object-oriented programming vs procedure-oriented programming language

Object-Oriented ProgrammingProcedure-Oriented Programming
Program is divided into objectsProgram is divided into function
It follows a bottom-up approachIt follows a top-down approach
It has the concept of virtual functionIt has no the concept of virtual function
inheritance is properly used in object-oriented programming inheritance is not supported by procedure-oriented programming
Oops vs PoPs

What is a Class?

A class is a collection of objects. Unlike primitive data structure, classes are data structure of user-defined, which is used to make a code more manageable. Let’s see how can we define a class in a python programming language.

class car:
     pass

you can define a class with the keyword “Class” following class name and semi-clone and you can write the body of the class by using indentation which makes code more manageable.

What is object?

When we create a class it only creates a description or blueprint of the object, so until we create an object there is no memory is allocated. Moreover, The object instance only contains the real data. Let’s create an object of the above program. You can change the name of an object according to your needs.

obj1=car()
print(obj1)
Output:
<__main__.car object at 0x7f270b465730>

Class constructor

Constructor plays an important role while creating a class. The job of the constructor is to assign values to the data member of the class while the object of the class is created. In python programming language we can use the __init__() method while creating a constructor, this method is also called the constructor method. The following program shows how the constructor assigns values to the data member.

class car:
    def __init__(self, name, cylinder, color):
        self.name=name
        self.cylinder=cylinder
        self.color=color

Method

Methods are functions that can be used to define the behaviour of the object. Additionally, methods are also written inside the class. The following program shows how can we write methods inside of class.

class car:
    def __init__(self, name, cylinder, color):
        self.name=name
        self.cylinder=cylinder
        self.color=color
    def Display(self):
        print("model of the car"+self.name)

if we use an instance variable in a method we need to use the self keyword

Creating more than one object of class

following the program show how can we create more than one object of a class.

obj1=car('BMW',4,'Red')
obj2=car("Tesla",0,"White")

Inheritance

Inheritance is the process by which one class inheritance the method and attribute from another class. The class from where methods and attributes are inherited is called parent class and likewise the class inheritance the property from the parent class is called the child class. The interesting thing about the inheritance property is child class has their own property and method after it inheritance from the parent. The following syntax is used to inheritance class in the python programming language.

class parent_class:
body of parent class

class child_class( parent_class):
body of child class

Encapsulation

Data Abstraction

Leave a Reply