0% found this document useful (0 votes)
4 views4 pages

PP 5

Uploaded by

Aahaan Phadnis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

PP 5

Uploaded by

Aahaan Phadnis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

PYTHON LAB MANUAL

EXPERIMENT NO.5
AIM: To create and apply classes and objects in python
Name : Adwaita Patane and Dhriti Ayyalasomayajula
Reg ID : 231061003 and 231061021

THEORY:
What Are Classes and Objects?
Classes
A class is a blueprint for creating objects. It defines the structure and behaviour (data and
methods) that the objects created from the class will have.
• Analogy: A class is like a blueprint for a building—it defines how the building should
look, but it's not the actual building.
• Components:
o Attributes: Variables that store data related to the object.
o Methods: Functions that define the behaviour of objects.
Objects
An object is an instance of a class. When a class is instantiated, an object is created, inheriting
all the attributes and methods defined by the class.
• Analogy: An object is like a specific building constructed from the blueprint.

Key Concepts in Classes and Objects


1. Encapsulation
Encapsulation is the bundling of data (attributes) and methods (functions) into a single unit,
i.e., the class. It restricts direct access to some of the object's components, ensuring controlled
access through methods.
• Benefit: Protects the internal state of an object and promotes modularity.
2. Inheritance
Inheritance allows a class (child class) to inherit attributes and methods from another class
(parent class). This promotes code reuse and hierarchy.
• Parent Class (Superclass): The class being inherited from.
• Child Class (Subclass): The class inheriting the properties.

3. Polymorphism
Polymorphism means "many forms." It allows methods in different classes to have the same
name but behave differently based on the class.
• Example in Context: A Vehicle class might have a start() method, but its subclasses like
Car and Bike can implement start() differently.

4. Abstraction
Abstraction focuses on hiding the implementation details and showing only the necessary
features of an object. This is often achieved through abstract classes or interfaces.
• Example in Context: A user interacting with a "printer" doesn't need to know how the
printer works internally; they just press "print."

5. Constructor (__init__ Method)


The constructor is a special method in Python that is automatically called when an object is
created. It initializes the attributes of the object.

Advantages of Using Classes and Objects


1. Code Reusability: Inheritance allows common functionality to be reused.
2. Modularity: Encapsulation organizes code into logical units.
3. Scalability: Easy to extend and maintain.
4. Improved Readability: Classes and objects make complex code more understandable.
Class Components
1. Attributes (Instance Variables): Store object-specific data.
2. Methods: Define the behavior of an object.
3. Class Variables: Shared by all instances of a class.
4. Instance Variables: Unique to each object.

Object Components
1. State: Represented by attributes.
2. Behavior: Defined by methods.
3. Identity: A unique identifier for each object (memory location).

PROGRAM OUTPUT:
CONCLUSION
Creating and applying classes and objects in Python is fundamental to mastering object-
oriented programming. Classes provide a blueprint for defining real-world entities with
attributes (data) and methods (behaviours), while objects are instances of these classes. This
approach promotes code reusability, modularity, and scalability, making it ideal for building
complex applications. By encapsulating data and behaviour within classes, Python enables
clean and organized code, while concepts like inheritance and polymorphism enhance
functionality and flexibility.

You might also like