Class and Object
Class and Object
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
class Parrot:
# class attribute
name = ""
age = 0
parrot1 = Parrot()
parrot1.name = "Abby"
parrot1.age = 10
parrot2 = Parrot()
parrot2.name = "Angel"
parrot2.age = 15
# access attributes
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.
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:
self.name = name
self.age = age
# access attributes
Example-2
Class faculty:
def display(self):
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.
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:
self.name = name
def fly(self):
print(f"{self.name} is flying.")
class Parrot(Bird):
super().__init__(name)
self.color = color
def fly(self):
def speak(self):
# Call methods
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 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
pass
Example:
# Parent class 1
class Animal:
def speak(self):
# Parent class 2
class Vehicle:
def move(self):
def bark(self):
dog1 = Dog()
Explanation:
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.