Jul 07, 2022 iOS

OOPS in Python: What are the Components of OOPS?

OOPS in Python: What are the Components of OOPS? 

Procedural programming has been the go-to programming approach when data is stored in variables and functions are run on the data. As part of your first programming course, you will learn how to program in this manner because it is straightforward.

But as your programs expand, you’ll wind up with a plethora of erratic functionalities. Problems arise when you alter one function, as several other functions will break as a result of the interdependency between all of these functions.

You can address this issue with object-oriented programming, as you can put together a collection of variables and functions using OOPS in Python. These variables are referred to as properties, and functions as methods. With the help of methods declared inside a class, you can invoke OOPS.

What the OOPS in Python Entails

Each distinct object represents a unique component of the application, with its own internal logic and data. Python’s classes, objects, methods, inheritance, polymorphism, data abstraction, and encapsulation are all OOPs in Python.

Class

An object’s class serves as a predefined or user-defined template. It displays the common attributes and operations shared by all objects belonging to the same class. Access modifiers, class names, interfaces, and class bodies are just a few of its many characteristics.

class MyClass:
  x = 5

p1 = MyClass()
print(p1.x)




Every class has a function called __init__(), and you can use them when you create a class or set values for object properties.

class Details:
  def __init__(self, sex, age):
    self.sex = sex
    self.age = age

p1 = Details("male", 26)

print(p1.name)
print(p1.age)

#output

male

26

Object

An object is a collection of diverse data and functions that work with data containing the following characteristics:

  • State
  • Behavior
  • Identity

Method

In Python, a method is a function you call on an object and is accessible for a certain object because of the object’s type. Additionally, you need to build it inside a class. A method, like a function, has a name, may accept parameters, and may contain a return statement.

Let’s take an example of this.

class Say_hi:
   def __init__(self):
      pass
   def printhello(self,name):
      print(f"Hello, {name}")
      return name
obj=Say_hi()
obj.printhello('Krishna')

#Output

Hello, Krishna

Krishna

Inheritance

With inheritance, you can create classes that inherit all the features of their parent classes and allow us to add new ones.

class Person(object):


    # __init__ is known as the constructor

    def __init__(self, subject, score):

        self.subject = subject 

        self.score= score 

    def display_inheri(self):

        print(self.subject)

        print(self.score)         

    def details(self):

        print("My best subject is {}".format(self.subject))

        print("And I scored: {}".format(self.score),"in my last Exams")

     
# child class

class Student(Person):

    def __init__(self, subject, score, Class):

        self.Class = Class


        # invoking the __init__ of the parent class

        Person.__init__(self, subject, score)

         

    def details(self):

        print("My best subject is {}".format(self.subject))

        print("score: {}".format(self.score))

        print("Class: {}".format(self.Class))


# creating an object instance

a = Student ('chemistry', 98, "Level 200")

a.display_inheri()
a.details()

#Output 

chemistry

98

My best subject is chemistry

score: 98

Class: Level 200

Polymorphism

Polymorphism is a Greek word meaning that various kinds of elements can use the same function name. As a result, programming is simpler and more logical. Polymorphism in programming entails the use of a single type entity (method, operator, or object) to represent several types in various contexts.

#Output 

Ants are of different species

Most of the ants are tiny and others huge.

 Ants are of different species

Sugar ants are always in groups.

 Ants are of different species

Soldier ants have a big head.

Data Abstraction

In Python, abstraction is the practice of managing complexity by concealing extra information from the user.

Encapsulation

One of the core ideas in OOPS is encapsulation. 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 can avoid data alteration.

Note: The programming paradigm known as object-oriented programming combines data and methods. OOPs in Python aid in reducing code complexity and promoting code reuse. It also encourages flexibility and long-term code maintenance.

Index