1) What is a class?¶
If you want to build multiple cars, you don't write instructions from scratch everytime--- you use a blueprint
class Car:
pass
This defined a class called Car
. It doesn't do anything yet-- it's just an empty plan.
2) Creating an Object¶
You use the blueprint to create real things --- objects
my_car = Car()
Now my_car
is an object (or instance) of the Car
class. You can make more cars.
another_car = Car()
3) Add Data with the __init__
Method¶
Let's give each car some info, like color or brand.
class Car:
def __init__(self, brand, color):
self.brand = brand
self.color = color
__init__
: Special method that runs when an object is created.
self
: Refers to the current object.
self.brand = brand
: Saves the brand info in the object.
# Now create a car:
my_car = Car("Toyota", "Red")
print(my_car.brand)
print(my_car.color)
Toyota Red
4) Add Behavior (Methods)¶
Classes can do things. Let's add a method.
class Car:
def __init__(self, brand, color):
self.brand = brand
self.color = color
def drive(self):
print(f"The {self.color} {self.brand} is driving")
# Use the method
my_car = Car("Honda", "white")
my_car.drive()
The white Honda is driving
5) Add more Logic¶
You can store static (info) and make changes:
class Car:
def __init__(self, brand, color):
self.brand = brand
self.color = color
self.speed = 0
def accelerate(self):
self.speed += 10
print(f"{self.brand} speed is now {self.speed} km/h.")
def drive(self):
print(f"{self.brand} is driving")
# Use the method again
my_car = Car("Ferrari", "Red")
my_car.accelerate()
Ferrari speed is now 10 km/h.
my_car.accelerate()
Ferrari speed is now 20 km/h.
6) Inheritance¶
Now you can make new classes based on old ones.
class ElectricCar(Car):
def charge(self):
print(f"{self.brand} is charging.")
# let's call the new class
tesla = ElectricCar("Tesla", "black")
tesla.charge()
Tesla is charging.
tesla.accelerate()
Tesla speed is now 10 km/h.
tesla.drive()
Tesla is driving
7) Polymorphism (Same Method, Different Behavior)¶
class Animal:
def speak(self):
print("some sound")
class Dog(Animal):
def speak(self):
print("woof!")
class Cat(Animal):
def speak(self):
print("Meow!")
animals = [Dog(), Cat(), Animal()]
for a in animals:
a.speak()
woof! Meow! some sound
🤔 So Does Dog or Cat Overwrite Animal?¶
Not exactly. Here's what actually happens:
The Dog class inherits from Animal, meaning it starts with all of Animal's attributes and methods.
When you define speak()
again in Dog, this is called overriding the method.
It does not overwrite the method in Animal; it replaces it only for objects of type Dog.
So when Python sees a.speak()
:
It checks the actual class of a.
If a is a Dog, it checks if Dog has a speak()
method.
If found, it runs that and ignores the parent version.
If not found, it moves up to the parent, Animal, and uses that version.
🧬 Visualizing the Lookup (Method Resolution Order - MRO)¶
plaintext
Dog().speak()
↓
Look in Dog → Found! Use Dog's speak()
Cat().speak()
↓
Look in Cat → Found! Use Cat's speak()
Animal().speak()
↓
Look in Animal → Found! Use Animal's speak()
✅ Key Concept: Method Overriding ≠ Overwriting
Animal.speak() still exists.
Dog and Cat just override it when their own version is called.
This is the power of polymorphism—different behaviors for the same method name depending on the actual object type.
8) Encapsulation¶
Encapsulation means hiding the internal details of how an object works and exposing only what's necessary through public methods.
It helps:
Prevent accidental changes to internal data
Control access with getter/setter methods
Make code easier to maintain and debug
class BankAccount:
def __init__(self, balance):
self.__balance = balance # Private variable
def deposit(self, amount):
self.__balance += amount
def get_balance(self):
return self.__balance
acct = BankAccount(100)
acct.deposit(50)
print(acct.get_balance())
150
✅ Object-Oriented Programming Core Pillars (OOP Summary)¶
Encapsulation – Hiding the data (like private balance)
Inheritance – Child class inherits from a parent
Polymorphism – Same method name, different behavior
Abstraction – Hiding complexity and showing only essentials