Cp-I - Chapter 4 Notes
Cp-I - Chapter 4 Notes
4.1 Classes
What is a Class?
class ClassName:
# Attributes and methods defined here
pass
Example:
class Car:
# The initializer method is called when we
create a new object
def __init__(self, make, model, year):
# Initializing the attributes
self.make = make # Car's brand,
like Toyota or Ford
self.model = model # Model of the
car, like Corolla or Mustang
Explanation:
● class Car:: This defines a new class called Car.
● __init__ Method: This special method, also called a
constructor, initializes the object’s attributes when an
object is created.
○ self is a reference to the current object, allowing
the class to store data specific to that object.
● Attributes: self.make, self.model, and self.year
are attributes, storing data specific to each car object.
● Method display_info: This method is defined to
display the car’s information.
4.2 Objects
What is an Object?
Example:
Using the Car class we created, let’s make an object and interact
with it.
Explanation:
● my_car = Car("Toyota", "Corolla", 2020):
Here, we create an object my_car from the Car class. This
object has its own make, model, and year values.
● Calling display_info(): By calling this method on
my_car, we get the car’s information displayed.
4.3 Methods
What is a Method?
Let’s add another method to the Car class to honk the horn.
class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
def display_info(self):
print(f"{self.year} {self.make}
{self.model}")
def honk(self):
print("Beep beep!") # A new method
for honking the horn
Explanation:
● honk Method: We added a new method honk, which
simply prints "Beep beep!" to simulate the car honking.
● Calling Methods: We call my_car.display_info()
and my_car.honk() to make the car object display its
information and honk.
Explanation:
Practical Example:
class Phone:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def display_details(self):
print(f"Phone: {self.brand}
{self.model}")
Keywords:
4.5 Inheritance
What is Inheritance?
Let’s create a Vehicle class and a Car class that inherits from
it.
class Vehicle:
def __init__(self, make, model):
self.make = make
self.model = model
def display_info(self):
print(f"Vehicle: {self.make}
{self.model}")
def display_info(self):
super().display_info() # Calls the
parent class's display_info method
print(f"Year: {self.year}")
Explanation:
● class Car(Vehicle):: The Car class inherits from
the Vehicle class, gaining its attributes and methods.
● super(): A keyword used to call a method from the
parent class. We use super().__init__ to initialize the
parent class attributes.
● The display_info method in the Car class also calls the
display_info method from Vehicle using super()
to show how we can extend a parent class's functionality in
a child class.
4.6 Polymorphism
What is Polymorphism?
class Animal:
def sound(self):
print("Animal makes a sound")
class Dog(Animal):
def sound(self):
print("Dog barks")
class Cat(Animal):
def sound(self):
print("Cat meows")
# Polymorphism in action
my_dog.sound() # Output: Dog barks
my_cat.sound() # Output: Cat meows
Explanation:
● Method Overriding: Both Dog and Cat classes override
the sound method from the Animal class. When we call
sound, the specific version for each subclass runs.
This makes code more flexible and easy to extend, as we can add
new classes without changing the parent structure.
Let’s create a Car class that has an Engine class as one of its
attributes.
class Engine:
def start(self):
print("Engine started")
class Car:
def __init__(self, make, model, engine):
self.make = make
self.model = model
self.engine = engine # Car contains
an Engine
def start_car(self):
print(f"Starting {self.make}
{self.model}")
self.engine.start() # Delegates
starting to the Engine
4.8 Reusability
What is Reusability?
4.9 Delegation
What is Delegation?
class BankAccount:
def __init__(self, balance):
self.__balance = balance # Private
attribute
def get_balance(self):
return self.__balance
Explanation:
4.11 Encapsulation
What is Encapsulation?
class BankAccount:
def __init__(self, balance):
self.__balance = balance # Private
attribute
def get_balance(self):
return self.__balance
Explanation: