python


Python OOP


As we know, Python is a multi-programming paradigm. It supports diffrent types of programming approaches in which OOP is one of them
OOP means Object oriented Programming.In this programming paradigm, focus is more on object rather than procedure.

What is Object?

Object is an identifiable entity which has some attributes and behaviour.
An object consists of-

Let's take an example:
Suppose I have a Car as object, as it has following properties:

In python, OOP follows some important principles:

Class

Class can be defines as a group of objects or a blueprint of objects.
We can think a Car as an class.

Syntax:
class classname:

To define a class we will useclass keyword as mentioned in the syntax.


Example:
classCar:

Here classname is Car.

Object

An object is defined as a instance of class.
When class is defined, only the description for the object is defined. Therefore, no memory or storage is allocated.
Example of object of class Car.

Example:
obj1=Car()

Suppose we have some datails about class CarAnd now we are building the class and objects of Car.

Example:

class Car:

    # class attribute
    type="vehicle"

    # instance attribute
    def __init__(self, name, color):
        self.name = name
        self.color = color

# instantiate the Parrot class
xuv= Car("XUV", "white")
verna = Car("Verna", "black")

# access the class attributes
print("XUV is a {}".format(xuv.__class__.type))
print("Verna is also a {}".format(verna.__class__.type))

# access the instance attributes
print("{} is {} in color".format( xuv.name, xuv.color))
print("{} is {} in color".format( verna.name, verna.color))
Output:
XUV is a vehicle
Verna is also a vehicle
XUV is white in color
Verna is black in color

In the above code, we created a class named Car.Then we define some attributes of an object.
These attributes are defined under __init__() method.It's a method which runs as the object is created.
Then we created the object of the class:xuv and verna.

Methods

Methods can be defined as the functions inside the class.


Example:

class Car:
    
# instance attributes
    def __init__(self, name, color):
        self.name = name
        self.color = color
    
# instance method
    def run(self, speed):
        return "{} runs at speed of {}km/h".format(self.name, speed)


# instantiate the object
xuv = Car("XUV","white")
verna=Car("Verna","Black")


# call our instance methods
print(xuv.run(70))
print(verna.run(60))
Output:
XUV runs at speed of 70km/h
Verna runs at speed of 60km/h

In the above code, we define a method run().

Inheritance

Inheritance can be defined as a way of creating a new class from a existing class without modifying it.The new class is be known as Derived class and the existing class is known as Base class.

Example:

# base class
class Car:
    
    def __init__(self):
        print("Car is ready")

    def whatType(self):
        print("Car")

    def speed(self):
        print("Speed fast")

# child class
class Diesel(Car):

    def __init__(self):
        # call super() function-The super() function in Python makes class inheritance more manageable and extensible.
        super().__init__()
        print("Diesal car is ready")

    def whatType(self):
        print("Diesel Car")

    def run(self):
        print("Run faster")

peggy = Diesel()
peggy.whatType()
peggy.speed()
peggy.run()
Output:

Car is ready
Diesal car is ready
Diesel Car
Speed fast
Run faster
Polymorphism

A simple definition of Polymorphism can be "One name in multiple form"
Suppose we need to find out the area of different shapes like circle,square etc.Although we can use same method to find out the area of any shape. This is called Polymorphism.

Example:

class Car:

    def speed(self):
        print("Car can speed faster")
    
    

class Bicycle:

    def speed(self):
        print("Bicycle can't speed faster")
    
    

# common interface
def speed_test(vehicle):
    vehicle.speed()

#instantiate objects
ecosport= Car()
hercules = Bicycle()

# passing the object
speed_test(ecosport)
speed_test(hercules)
Output:

Car can speed faster
Bicycle can't speed faster

In the above code, we defined 2 classes Car and Bicycle.Both class uses the same method speed() but their functions are different.