Why We Use Oops Concept In Python Programming

Reading Time: 5 minutes

INTRODUCTION

Python is a high-level general-purpose and very popular programming language. It allows us to develop applications using an Object-Oriented approach. In Python, we can easily create and use classes and objects. By which it focuses on writing the reusable code. It is a widespread technique to solve the problem by making things. The latest Python 3 version is used in web development, Machine Learning applications, and all cutting-edge technology in the software industry. It is a healthy suite for Beginners and experienced programmers with other programming languages like C++ and Java.

Basically, python is not a fully Object-oriented language like java or some other programming language. It means python works fine with or without the oops concept with reliable and short syntax and provide the following benefits to the coder

  • Easy to Learn & Code
  • Free and High Level Language
  • Portable
  • Large Collection of Packages and Modules
  • Interpreted

Basic syntax

The first program of python :

print("hello, world!")

Output: 

hello, world!

Why object-oriented (oops)concept here

so when everything works fine then why do we need oops in python as we all know oops(object-oriented programming) also provide some benefits to programming languages like

  • Modularity for easier troubleshooting
  • Reuse of code through inheritance
  • Flexibility through polymorphism
  • Effective problem solving ,etc

These concepts make python a more special, famous, and modern language. let`s start a detailed study of how oops pillars work in python.

Oops Concept

Python is a great programming language that supports Object-Oriented Programming systems is call  OOPS

There are four pillars of Oops:

  1. Encapsulation
  2. Inheritance
  3. Polymorphism
  4. Abstraction

CLASS

class is a blueprint that defines the variables and the methods common to all objects of a certain kind. To define a class in Python, you can use the keyword.

#class declaration
class Person:

    def __init__(self):
        pass

OBJECT

An object is a software bundle of variables and related methods which represent real-world entities. Objects have states and behavior. To instantiate an object, type the class name, followed by two brackets.

#object declaration
p = Person()

example

CLASS AND OBJECT WITH EXAMPLE IN REAL WORLD - YouTube

Methods in a class

A method is a function that “belongs to” an object. Methods are defined within a class, we use the def keyword to define a method in a class.


    def __init__(self, name, age):  
        self.name = name
        self.age = age

    def say_hi(self):
        print("Hello")

Example:

class Person:
   
    # init method or constructor 
    def __init__(self, name):
        self.name = name
   
    # Sample Method 
    def say_hi(self):
        print('Hello, my name is', self.name)
   
p = Person('Rahul')
p.say_hi()

Output:

  Hello, my name is Rahul

Encapsulation

It describes the idea of wrapping data and the methods that work on data within one unit. This puts restrictions on accessing variables and methods directly and can prevent the accidental modification of data

Encapsulation: To enclose something in or as if in a capsule

Learn Object Oriented Programming Using C#: Part 5
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()

In the above code, to change the value of the price of the computer you can only do it by using the set function or using the underscore.

Output:

Selling Price: 900
Selling Price: 900
Selling Price: 1000

Inheritance

Inheritance is the capability of one class to derive or inherit the properties from an added class. 

Inherit: To receive a quality, characteristic, etc., from your parents or family.

 The benefits of inheritance are: 

  1. It represents real-world relationships well.
  2. It provides the reusability of a code. We do not have to write the same code again and again. It allows us to add more features to a class without modifying it.
  3. It is transitive, which means that if class B inherits from added class A, all the subclasses of B would automatically inherit from class A.
Inheritance In Java. Inheritance in real world example that… | by  Yaminbaloch | Medium
class Parent():
       def first(self):
           print('first function')
 
class Son(Parent):
       def second(self):
          print('second function')
 
ob = Son()
ob.first()
ob.second()

Output:

first function
second function

Abstraction

Abstraction is used to hide the internal functionality of the function from the users. The users only interact with the basic implementation of the function; inner working is hidden. User is familiar with that “what function does” they do not know “how it does.”

Abstraction: A general idea in place of one that relates to a particular object, person, or situation.

What Is Abstraction In Java - Learn With Examples
#Abstract Class
class Vehicle:
    def start(self,name=""):
        print(name,"is Started")
    def acclerate(self,name=""):
        pass
    def park(self,name=""):
        pass
    def stop(self,name=""):
        print(name,"is stopped")

class Bike(Vehicle):
    def acclerate(self, name=""):
        print(name,"is accelrating @ 60kmph")
    def park(self, name=""):
        print(name,"is parked at two wheeler parking")

class Car(Vehicle):
    def acclerate(self, name=""):
        print(name,"is accelrating @ 90kmph")
    def park(self, name=""):
        print(name,"is parked at four wheeler parking")


def main():
    print("Bike Object")
    b=Bike()
    b.start("Bike")
    b.acclerate("Bike")
    b.park("Bike")
    b.stop("Bike")

    print("\nCar Object")
    c = Car()
    c.start("Car")
    c.acclerate("Car")
    c.park("Car")
    c.stop("Car")
if __name__=="__main__":main()

Output:

Bike Object
Bike is Started
Bike is accelrating @ 60kmph 
Bike is parked at two wheeler parking
Bike is stopped
 
Car Object 
Car is Started 
Car is accelrating @ 90kmph
Car is parked at four wheeler parking
Car is stopped 

Polymorphism

The word polymorphism means having many forms. In programming, polymorphism means the same function name (different signatures) being use for different types.

Polymorphism: The condition of occurrence in several different forms.

Polymorphism in Java
class Dog:
    def Speak(self):
        print("Woof Woof")
class Cat:
    def Speak(self):
        print("Meow meow")
class AnimalSound:
    def Sound(self, animal):
        animal.Speak()

Output:

woof woof
Meow mwow

That’s all for this blog, I Hope so learned about python and why we use the oops concept in python, and in the next blog, we will learn more about python so stay tuned.

Thank you!!  

References

https://www.python.org/

https://www.roberthalf.com/blog/salaries-and-skills/4-advantages-of-object-oriented-programming

knoldus

Written by 

I am a Software Consultant in knoldus software limited, I have 6 months past experience and I am a quick learner, self-motivated, and etc