Python Object Oriented Programming
Python is a versatile programming language that supports various programming styles, including
object-oriented programming (OOP) through the use of objects and classes.
An object is simply a collection of data (variables) and methods (functions). Similarly, a class is a
blueprint for that object.
We can think of the class as a sketch (prototype) of a house. It contains all the details about the
floors, doors, windows, etc.
An object is any entity that has attributes and behaviors. For example, a parrot is an object. It has
attributes - name, age, color, etc.
behavior - dancing, singing, etc.
Similarly, a class is a blueprint for that object.
class Parrot:
# class attribute
name = ""
age = 0
# create parrot1 object
parrot1 = Parrot()
parrot1.name = "Abby"
parrot1.age = 10
# create another object parrot2
parrot2 = Parrot()
parrot2.name = "Angel"
parrot2.age = 15
# access attributes
print(f"{parrot1.name} is {parrot1.age} years old")
print(f"{parrot2.name} is {parrot2.age} years old")
Explination
The code you provided defines a Parrot class with class-level attributes (name and age), and then
creates two objects parrot1 and parrot2
The class Parrot has two class attributes: name and age.
we create two instances (parrot1 and parrot2) of the Parrot class.
For each instance,we modify the name and age attributes individually.
Finally, print the name and age of each parrot.
To fix this, we can define the attributes within the __init__() method, making them instance
attributes.
class Parrot:
# constructor to initialize instance attributes
def __init__(self, name, age):
self.name = name
self.age = age
# create parrot1 object
parrot1 = Parrot("Balu", 10)
# create another object parrot2
parrot2 = Parrot("Woo", 15)
# access attributes
print(f"{parrot1.name} is {parrot1.age} years old")
print(f"{parrot2.name} is {parrot2.age} years old")
Example-2
Class faculty:
def putdata (self):
Self . id=int(input(“enter faculty id”))
Self . name=input(“enter faculty name”)
Self . salary=float(input(“enter faculty salary”))
def display(self):
Print(“faculty id:” ,self . id)
Print(“faculty name:” ,self . name)
Print(“faculty salary:” ,self . salary)
a=faculty()
a.putdata()
a.display()
Use of Inheritance in Python
Inheritance in Python is a feature that allows a class (called a child class or subclass) to inherit
attributes and methods from another class (called a parent class or superclass). This allows you
to reuse code, make your program more modular, and follow the DRY (Don't Repeat Yourself)
principle by reducing redundancy.
Key Benefits of Inheritance:
1. Code Reusability: You can reuse the functionality of an existing class in a new class.
2. Extensibility: You can add new features to an existing class without modifying it.
3. Simplifies Maintenance: Changes made in the parent class are reflected in the child class,
which simplifies updating and maintaining the code.
4. Method Overriding: Child classes can provide a specific implementation of methods already
defined in the parent class
Example of Inheritance
# Parent class
class Bird:
def __init__(self, name):
self.name = name
def fly(self):
print(f"{self.name} is flying.")
# Child class inheriting from Bird
class Parrot(Bird):
def __init__(self, name, color):
# Call parent class constructor
super().__init__(name)
self.color = color
# Override the fly method
def fly(self):
print(f"{self.name}, the {self.color} parrot, is flying.")
# New method specific to Parrot
def speak(self):
print(f"{self.name} says 'Hello!'")
# Create an instance of Parrot
parrot = Parrot("Kittu", "blue")
# Call methods
parrot.fly() # Overridden fly method from Parrot class
parrot.speak() # Method specific to Parrot class
Explanation:
Parent Class (Bird): Contains basic functionality like fly(). Any bird can fly, so we put this
behavior in the base class.
Child Class (Parrot): Inherits the fly() method from Bird but also extends it by adding a color
attribute and a new method speak(). The fly() method is overridden to make it specific to a
parrot.
Multiple Inheritance in Python
Multiple inheritance is a feature in object-oriented programming where a class can inherit attributes
and methods from more than one parent class. In Python, this allows a class to have multiple base
classes.
class BaseClass1:
# Base class 1
pass
class BaseClass2:
# Base class 2
pass
class DerivedClass(BaseClass1, BaseClass2):
# Derived class inheriting from BaseClass1 and BaseClass2
pass
Example:
# Parent class 1
class Animal:
def speak(self):
return "Animal sound"
# Parent class 2
class Vehicle:
def move(self):
return "Vehicle is moving"
# Child class inheriting from both Animal and Vehicle
class Dog(Animal, Vehicle):
def bark(self):
return "Dog is barking"
# Creating an object of the child class
dog1 = Dog()
# Accessing methods from both parent classes
print(dog1.speak()) # Inherited from Animal
print(dog1.move()) # Inherited from Vehicle
print(dog1.bark()) # Defined in Dog class
Explanation:
Animal is the first base class, with a method speak().
Vehicle is the second base class, with a method move().
Dog is the derived class that inherits from both Animal and Vehicle, and it has its own
method bark().
When an object of Dog is created, it can access methods from both parent classes and its
own method.