You are on page 1of 8

Lecture: Python Object-Oriented

Programming (OOP)
Introduction to Object-Oriented Programming (OOP)
Object-Oriented Programming (OOP) is a programming paradigm based on the concept of
"objects", which can contain data in the form of attributes and code in the form of methods.
Python is an object-oriented language, which means it supports OOP principles.

Creating a Class and Object:

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}")

# Creating objects of the Car class

car1 = Car("Toyota", "Corolla", 2020)

car2 = Car("Honda", "Civic", 2018)

# Accessing object attributes and calling methods

car1.display_info()

car2.display_info()
Inheritance is one of the fundamental concepts in object-oriented programming (OOP). It allows
a new class to inherit attributes and methods from an existing class, promoting code reuse and
facilitating the creation of specialized classes. In Python, inheritance is achieved by specifying
the parent class(es) in the definition of a subclass.

1. Superclass (Parent Class):


• The class whose attributes and methods are inherited by another class is called
the superclass or parent class.
2. Subclass (Child Class):
• The class that inherits attributes and methods from a superclass is called the
subclass or child class.
3. Single Inheritance:
• In single inheritance, a subclass inherits from only one superclass.

4. Multiple Inheritance:
• Python supports multiple inheritance, where a subclass can inherit from multiple
superclasses. However, it's recommended to use multiple inheritance cautiously
due to potential complexity and ambiguity.
5. Method Overriding:
• Subclasses can override methods of the superclass by redefining them in the
subclass. This allows for customizing the behavior of inherited methods in the
subclass.

Example of Inheritance in Python:

# Parent class
class Animal:
def __init__(self, name):
self.name = name

def speak(self):
print("Animal speaks")
# Child class inheriting from Animal
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name)
self.breed = breed

# Overriding the speak method


def speak(self):
print(f"{self.name} barks")

# Child class inheriting from Animal


class Cat(Animal):

def __init__(self, name, color):


super().__init__(name)
self.color = color

# Overriding the speak method


def speak(self):
print(f"{self.name} meows")

# Creating objects of the subclasses


dog = Dog("Buddy", "Golden Retriever")
cat = Cat("Whiskers", "Gray")

# Accessing inherited attributes and methods


print(f"{dog.name} is a {dog.breed}")
dog.speak()

print(f"{cat.name} is {cat.color}")
cat.speak()

Example:
class ElectricCar(Car):
def __init__(self, make, model, year, battery_capacity):
super().__init__(make, model, year)

self.battery_capacity = battery_capacity

def display_info(self):
super().display_info()
print(f"Battery Capacity: {self.battery_capacity} kWh")

# Creating an object of the ElectricCar class


electric_car = ElectricCar("Tesla", "Model S", 2022, 100)

# Accessing inherited attributes and overridden method


electric_car.display_info()
Exercise Task:

Objective: Implement classes using inheritance and method overriding in Python.

Instructions:

1. Define a parent class called Animal with the following attributes and methods:

• Attributes:

• name: representing the name of the animal.

• Methods:

• __init__(self, name): constructor method to initialize the name attribute.

• speak(self): method to display a generic message indicating the animal speaks.

2. Create two subclasses Dog and Cat inheriting from the Animal class. Implement the following:

• Dog class:

• Attributes:

• breed: representing the breed of the dog.

• Methods:

• __init__(self, name, breed): constructor method to initialize the name


and breed attributes.

• speak(self): method to display a message indicating the dog barks.

• Cat class:

• Attributes:

• color: representing the color of the cat.

• Methods:

• __init__(self, name, color): constructor method to initialize the name


and color attributes.

• speak(self): method to display a message indicating the cat meows.

3. Instantiate objects of the Dog and Cat classes with appropriate attributes and call the speak()
method for each object to display their respective sounds.
Example:

# Define the Animal, Dog, and Cat classes as instructed.

# Create objects of Dog and Cat classes

dog = Dog("Buddy", "Golden Retriever")

cat = Cat("Whiskers", "Gray")

# Call speak() method for each object

dog.speak() # Output: Buddy barks

cat.speak() # Output: Whiskers meows

Advanced Exercise Task:


Objective: Implement classes using multiple inheritance, method overriding, and polymorphism
in Python.
Instructions:
1. Define a parent class called Vehicle with the following attributes and methods:
• Attributes:
• brand: representing the brand of the vehicle.
• Methods:
• __init__(self, brand): constructor method to initialize the brand attribute.
• display_info(self): method to display information about the vehicle,
including its brand.
2. Create two parent classes EngineType and FuelType representing the type of engine and
fuel of a vehicle, respectively. Each class should have an attribute and a method as
follows:
• EngineType class:
• Attribute:
• engine_type: representing the type of engine (e.g., "Gasoline",
"Electric").
• Method:
• get_engine_type(self): method to return the type of engine.
• FuelType class:
• Attribute:
• fuel_type: representing the type of fuel (e.g., "Petrol", "Diesel").
• Method:
• get_fuel_type(self): method to return the type of fuel.
3. Create a subclass called Car that inherits from the Vehicle, EngineType, and FuelType
classes. Implement the following:
• Attributes:
• model: representing the model of the car.
• Methods:
• __init__(self, brand, model, engine_type, fuel_type): constructor
method to initialize the brand, model, engine_type, and fuel_type
attributes.
• display_info(self): method to display detailed information about the car,
including its brand, model, engine type, and fuel type.
4. Instantiate objects of the Car class with different attributes and call the display_info()
method for each object to display their information.

Example:
# Define the Vehicle, EngineType, FuelType, and Car classes as instructed.
# Create objects of Car class
car1 = Car("Toyota", "Camry", "Gasoline", "Petrol")
car2 = Car("Tesla", "Model 3", "Electric", "Electricity")

# Call display_info() method for each car object


car1.display_info() # Output: Toyota Camry | Engine Type: Gasoline | Fuel Type: Petrol
car2.display_info() # Output: Tesla Model 3 | Engine Type: Electric | Fuel Type: Electricity
Note:

• Ensure that the display_info() method in the Car class overrides the method in the
parent class (Vehicle) to include information about the engine type and fuel type.

• Test your implementation with different instances of Car objects to verify the
correctness of the behavior.

You might also like