Python OOPs Concepts



What is OOPs in Python?


OOPs stands for object oriented programming.

This is a feature/concept which is generally provided by most of the languages.

This concept is helps to make real-world applications using python.

OOPs helps in making a code so smaller and writing the resuable code.

OOPs is a programming concept that uses some other technical terms to work and these technical terms are necessary to understand before one starts using oops concepts. Technical terms are like–

  • class
  • object
  • method
  • inheritance
  • polymorphism
  • encapsulation
  • data-abstraction

We will try to cover everything in detail with easy explanations.


Class


Class is called as the blueprint for the object. If you want to create a class then you need to use class keyword then write the name of the class and finally give a colon(:).


Syntax:

class ClassName:
    statemen-1
    statemen-2
    statemen-3
             .
             .
             .
    statement-n

A class contains class attributes, class constructor, class methods, etc.

For example, Parrot is an object of class bird that can have attributes like name, age, color, etc.


Object


Objects are the instantiation of classes and also the way to use classes.

These are those entities that have attributes and behaviors which are defined inside the class.

In real-word, object examples are desktop, mobile, ac, freeze, etc.

When we create a class, then we only describe the blueprint for objects but memory is allocated when we create objects of classes.


Syntax

class ClassName:
    pass
obj = ClassName()

Here, ClassName is the name of the class, and obj is the object of the class.


Example

class Employee:
    # class attribute
    company = "violet-cat-415996.hostingersite.com"
    # constructor
    def __init__(self, name, age, salary):
        # instance attributes
        self.name = name
        self.age = age
        self.salary = salary

# creating objects
emp1 = Employee("John", 34, 50000)
emp2 = Employee("Harry", 30, 60000)

# accessing class attributes using __class__ method
# syntax is-- instance.__class__.attribute
print(f"{emp1.name} and {emp2.name} work for {emp1.__class__.company}")

# accessing instance attributes
# syntax is-- instance.instance_attribute
print(f"{emp1.name}'s age is {emp1.age} and salary is {emp1.salary}")
print(f"{emp2.name}'s age is {emp2.age} and salary is {emp2.salary}")

Output

John and Harry work for violet-cat-415996.hostingersite.com
John's age is 34 and salary is 50000
Harry's age is 30 and salary is 60000

Explanation


Class Attribute

We create a class with name “Employee“. Variables that we create in a class are called class attributes and here, the company is a class attribute.

Constructor

Then, we have created the constructor. A constructor is a special method in a class that the python always calls when we create or instantiate an object. Python uses “__init__()” to create a constructor.

The self parameter

The __init__() method can take parameters but the first parameter should always be the “self” parameter which refers to the current instance of the class.

You can use any name instead of self but it should be the first parameter always.

Instance Attribute

Variables that we create inside a constructor are called instance attributes. These attributes are different for different instances. Like here name, age, and salary are different for both emp1 and emp2 instances.

Methods

Methods are the functions that are defined inside the body of a class and associated with an object. They are used to define the behaviors of an object.

Example

class Employee:
    # class attribute
    company = "violet-cat-415996.hostingersite.com"
    # constructor
    def __init__(self, name, age, salary):
        # instance attributes
        self.name = name
        self.age = age
        self.salary = salary
    # class method
    def myMethod1(self):
        print(f"Hi {self.name}!")
    def myMethod2(self, city):
        print(f"{self.name}, do you lives in {city}?")

# creating objects
emp1 = Employee("John", 34, 50000)
emp2 = Employee("Harry", 30, 60000)

# calling methods
emp1.myMethod1()
emp1.myMethod2("London")

Output

Hi John!
John, do you lives in London?

Here, we have created two methods mymethod1 and mymethod2, we can use self parameter with them.




Also Read: