0% found this document useful (0 votes)
5 views

Lecture 3

Uploaded by

Muwanga Habibu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lecture 3

Uploaded by

Muwanga Habibu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

YMCA COMPREHENSIVE INSTITUTE

Object Oriented Programming

LESSON 3_1
Inheritance

© YCI – 2025.
BIT 1203 2/22/2025
What is Inheritance?
2

◻ Definition:
Mechanism where a child class (subclass) inherits
attributes/methods from a parent class
(superclass).
◻ Key Benefits:
Code reusability.
Hierarchical modeling (e.g., Dog is an Animal).

© YCI – 2025. 2/22/2025


Basic Syntax … Code Example:
3

◻ Parent class
class ParentClass:
def parent_method(self):
print("Parent method")
◻ Child class Inherits from ParentClass
class ChildClass(ParentClass):
def child_method(self):
print("Child method")

© YCI – 2025. 2/22/2025


Types of Inheritance
4

◻ Single Inheritance:
class Parent: ...
class Child(Parent): ...
◻ Multiple Inheritance:
class Father: ...
class Mother: ...
class Child(Father, Mother): ...
◻ Multilevel Inheritance:
class Grandparent: ...
class Parent(Grandparent): ...
class Child(Parent): ...

© YCI – 2025. 2/22/2025


Overriding Methods
5

◻ Concept:
Redefine a parent method in the child class.
◻ Example:
class Animal:
def speak(self):
print("Generic animal sound")

class Dog(Animal):
def speak(self): # Override
print("Bark!")
© YCI – 2025. 2/22/2025
The super() Function
6

◻ Purpose:
Call parent class methods from the child.
◻ Example (Constructor):
class Vehicle:
def __init__(self, type):
self.type = type

class Car(Vehicle):
def __init__(self, type, brand):
super().__init__(type) # Call parent's __init__
self.brand = brand
◻ Use Case:
Avoid rewriting parent logic.

© YCI – 2025. 2/22/2025


Method Resolution Order (MRO)
7

◻ What is MRO?
The order in which Python searches for methods in
inheritance hierarchies.
◻ Check MRO:
print(ChildClass.__mro__)
◻ Example (Multiple Inheritance):
class A: pass
class B(A): pass
class C(A): pass
class D(B, C): pass
print(D.__mro__) # Order: D → B → C → A → object

© YCI – 2025. 2/22/2025


Practical Example
8

◻ Employee Management System:


class Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary
def display_info(self):
print(f"{self.name}: ${self.salary}")
class Manager(Employee):
def __init__(self, name, salary, team_size):
super().__init__(name, salary)
self.team_size = team_size
def display_info(self): # Override
super().display_info()
print(f"Team Size: {self.team_size}")

© YCI – 2025. 2/22/2025


Best Practices
9

◻ Use super() for parent class initialization.


◻ Avoid deep inheritance hierarchies (keep it
shallow).
◻ Prefer composition over inheritance if no "is-a"
relationship.
◻ Document overriding methods clearly.

© YCI – 2025. 2/22/2025


Common Pitfalls
10

◻ Diamond Problem: Ambiguity in multiple


inheritance.
◻ Overcomplicating: Using inheritance where a
function or module would suffice.
◻ Ignoring MRO: Unintended method calls in
complex hierarchies.

© YCI – 2025. 2/22/2025


Example
11

◻ Transport
Source
Destination
Fare
Time of travel
◻ By road
Car
Bus
bicycle
◻ By air
Boeing
Bombardier
◻ By rail
Steam train
Electric train

© YCI – 2025. 2/22/2025


Thank you

© YCI – 2025. 2/22/2025

You might also like