Classes and Objects
Imagine you have a box. This box is special because it can hold different things like toys, books, or anything you like. In programming, we have something similar called a “class.”
What is a Class?
A class is like a blueprint or template. Imagine you have a blueprint for building a house. It tells you what the house should look like and what rooms it should have. Similarly, a class tells the computer what properties and actions an object should have when it’s created.
Structure of a Class in Python:
Syntax:
class ClassName:
# Execution code like methods, fields, constructor, etc.
#fieldName
#def methodName()
What is an Object?
Now, imagine using that blueprint to create an actual car. That car you create is called an “object.”
An object is an actual instance or copy of a class. It has unique properties (like the color of the car) and behaviors (like driving or stopping).
Structure of an Object in Python:
Syntax:
objectName = ClassName() # Creating an object
objectName.fieldName = value # Accessing fields
objectName.methodName() # Calling methods
Example: Car Class and Objects in Python
Let’s build a simple Car class and create objects from it.
class Car:
# Method to drive the car with color and speed
def drive(self, color, speed):
print(f”The {color} car driving speed is {speed}”)
first_car = Car()
first_car.colour = “Red”
first_car.speed = 60
second_car = Car()
second_car.colour = “Blue”
second_car.speed = 200
first_car.drive(first_car.colour, first_car.speed)
#Output: The Red car driving speed is 60
second_car.drive(second_car.colour, second_car.speed)
#Output: The Blue car driving speed is 200
Explanation:
class Car:
This defines a simple class for a car.
def drive(self, color, speed):
This is a method inside the Car class. The drive method represents an action that the car can perform. It takes three parameters:
self: Refers to the current object (instance) of the class. It allows access to the attributes and methods of the object.
color: Represents the color of the car.
speed: Represents the speed of the car.
print(f”The {color} car driving speed is {speed}”)
This prints out the color and speed of the car in a formatted string using an f-string. For example, if the color is “Red” and speed is 60, it will print: “The Red car driving speed is 60”.
first_car = Car()
This creates an instance (or object) of the Car class and assigns it to the variable first_car. This means first_car is now a car object that can use the methods and attributes defined in the Car class.
first_car.colour = “Red”
This creates a new attribute color for the first_car object and assigns the value “Red” to it.
first_car.speed = 60
This creates another attribute speed for the first_car object and assigns the value 60 to it.
second_car = Car()
This creates another instance of the Car class, this time named second_car. Now, we have two separate car objects, first_car and second_car.
second_car.colour = “Blue”
Similar to the first_car, this line sets the color attribute of the second_car object to “Blue”.
second_car.speed = 200
This sets the speed attribute of the second_car object to 200.
first_car.drive(first_car.colour, first_car.speed)
This calls the drive method on the first_car object.
The method is passed two arguments: first_car.colour (which is “Red”) and first_car.speed (which is 60).
Inside the drive method, it prints the message: “The Red car driving speed is 60”.
second_car.drive(second_car.colour, second_car.speed)
This calls the drive method on the second_car object.
The method is passed second_car.colour (which is “Blue”) and second_car.speed (which is 200).
Inside the drive method, it prints the message: “The Blue car driving speed is 200”.
Output:
The Red car driving speed is 60
The Blue car driving speed is 100
Example2: Create a class named Student with the following properties:
class Student:
def display(self, name, roll, course):
print(f”Name: {name}”)
print(f”Roll Number: {roll}”)
print(f”Course: {course}”)
# Create an object of the Student class
student1 = Student()
# Assign values to the properties
student1.name = “Jane Doe”
student1.rollNumber = “12345”
student1.course = “Computer Engineering”
# Print the properties of the object
student1.display(student1.name, student1.rollNumber, student1.course)
Explanation:
class Student:
defines a new class named Student.
def display(self, name, roll, course):
defines a method named display that takes self (the instance of the class) and three parameters (name, roll, course).
print(f”Name: {name}”)
print(f”Roll Number: {roll}”)
print(f”Course: {course}”)
Inside the display method, the print statements output the values of name, roll, and course.
student1 = Student()
This line creates an instance of the Student class named student1.
# Assign values to the properties
student1.name = “Jane Doe”
student1.rollNumber = “12345”
student1.course = “Computer Engineering”
These lines assign values to the attributes name, rollNumber, and course of the student1 object. Note that these attributes are not defined in the Student class, so they are dynamically added to the instance.
student1.display(student1.name, student1.rollNumber, student1.course)
This line calls the display method on the student1 object, passing the values of student1.name, student1.rollNumber, and student1.course as arguments to the method. The display method then prints these values.
Task:
1. Create a class named Employee with the following properties:
name
id
salary
Create an object of the Employee class.
Assign values to the properties and print them.
2. Create a class named Product with the following properties:
productName
category
price
Create an object of the Product class .
Assign values to the properties and print them .
3. Create a class named Student with the following properties:
name
rollNumber
course
Create an object of the Student class.
Assign values to the properties and print them.
4. Create a class named Car with the following properties:
make
model
year
Create an object of the Car class .
Assign values to the properties and print them.
5. Create a class named Book with the following properties:
title
author
pages
Create an object of the Book class in the .
Assign values to the properties and print them