Classes, Objects, and Instances
Lesson Overview
Classes are like blueprints that define how something should be structured and what it can do. Objects are real examples created using those blueprints. Each object has its own unique data but shares the structure and behaviour defined by its class. Instances are just specific objects created from a class. Learning about classes, objects, and instances helps you organize your code to make it easier to build, understand, and reuse
Lesson Content
Car Construction Analogy
Lets think of Programming in Terms of Building Cars
When starting to learn OOP, it can be difficult to imagine abstract concepts like classes and objects. Using a real-world, familiar example like car manufacturing helps build intuition.
In a car factory:
- There is a design plan or blueprint showing what a car should have—number of tires, model number, colour options, and engine type.
- The factory uses this design to produce individual cars.
- Each car model produced may have unique features(colour, engine type..) but follows the same basic design(blue print) for Car Production.
This mirrors how classes and objects work in programming, making it easier to understand before diving into code.
The Class: The Car Blueprint
The class is like the blueprint for a car. It defines the general characteristics and functionality that every car of this model will have.
Lets focus on characteristics of a car, when we define a Car, we focus on characteristics (attributes) that make it a car, such as colour, model name, fuel efficiency, engine type, and gear transmission etc...
It is crucial to understand that characteristics are specific to the entity you are modelling. For example:
- Petals and petal size are characteristics of a Flower, not a car.
- Steel grade and concrete type are characteristics of a Building or Bridge, not a car.
In Object-Oriented Programming, identifying the correct characteristics is the first step because they vary entirely based on the real-world entity (object) you are representing. You must choose properties that define what that specific object is.
Syntax
#class name/ blue print name
class class_name:
# The __init__ method is a special method (constructor) that runs automatically
def __init__(self, parameter1, parameter2):
self.parameter1 = parameter1
self.parameter2 = parameter2Car Class - Blueprint
#Class/Blue print Name : Car
class Car:
#Takes 3 main parameters : color, model_name
def __init__(self, color, model_name, engine_type):
self.color = color
self.model_name= model_name
self.engine_type= engine_typeThe Object: The Actual Car
The Class is the blueprint (the code definition on file). It describes the characteristics (attributes) and behaviours (methods) but isn't the physical entity itself. whereas the Object is the physical car (the instance in memory). It is the concrete, usable entity created from that blueprint, possessing specific values for those characteristics (like a red colour or model name).
Analogy: From Blueprint to Reality
- A blueprint is just a paper plan, but when you follow it and add specific details like paint and parts, you produce a physical car that exists in the real world.
- A Class is just a code definition (template), but when you provide specific parameters (data) to it, the computer creates an Object that occupies actual memory.
#pass the respective arguements to Class to create an Object
car1 =Car("Red", "Model X", "Robo123" )
car2 =Car("Blue", "Model Y", "Enthrean234" )Note : Multiple Objects can be created using a Class
The Instance: Each Particular Car
Using a single Class (blueprint), we can produce countless distinct physical Objects (cars). While every car is built from the same plan, each one holds its own unique data—like a different colour, engine type, or model number—making it an independent entity in memory. Therefore, Each of these cars is an instance of the Car blueprint. It’s like saying this car "belongs to" or "was made from" the Car class.
Just as we can observe a real car to see its unique details (like colour or engine), in Python we can retrieve these specific details (characteristics) directly from an object. This is done using dot notation. Since each object holds its own data, retrieving the same characteristics from different objects will give different results based on how they were created or changed.
# Retrieving characteristics (observing the cars)
print(car1.color) # Output: Red (Unique to car1)
print(car2.color) # Output: Blue (Unique to car2)
# The output depends on the specific object's data!
Summary
Classes help you define the general idea of something. Objects are tangible things created based on this idea in your program. Instances specify which object belongs to which class. Together, these concepts let you build organized, reusable, and scalable programs by mirroring how things exist and behave in the real world.