Object Oriented Programming (OOP)
Object Oriented Programming (OOP)
Scheme of work
Week 1: Introduction to Programming Paradigms & OOP Concepts
Week 5: Inheritance
Week 6: Polymorphism
Notes
Week 1: Introduction to Programming Paradigms & OOP Concepts
2. Why OOP?
OOP simplifies complex software design by using real-world analogies (like objects,
which represent things like cars or users).
Encourages reusability, modularity, and maintainability of code.
1. What is a Class?
python
Copy code
class Car:
def __init__(self, brand, color):
self.brand = brand
self.color = color
def start(self):
print(f"The {self.color} {self.brand} car is starting.")
2. What is an Object?
python
Copy code
my_car = Car("Toyota", "red")
my_car.start()
python
Copy code
class BankAccount:
def __init__(self, balance):
self.__balance = balance # private variable
def get_balance(self):
return self.__balance
2. Abstraction: #think of it like art just appreciate it rather than knowing its purpose
Abstraction hides the complex implementation details and shows only the necessary
parts.
Example: You don’t need to know the internals of how a start() method works, just
how to use it.
Constructors are special methods used to initialize objects when they are created.
Example:
python
Copy code
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
2. Destructors:
Week 5: Inheritance
Inheritance allows one class (derived/child class) to inherit attributes and methods from
another class (base/parent class).
Example:
python
Copy code
class Animal:
def sound(self):
print("This animal makes a sound.")
class Dog(Animal):
def sound(self):
print("The dog barks.")
2. Overriding Methods:
The Dog class can override the sound method from the Animal class to give its own
specific behavior.
Example:
python
Copy code
my_dog = Dog()
my_dog.sound() # Output: "The dog barks."
Week 6: Polymorphism
1. Polymorphism Definition:
Polymorphism allows methods to do different things based on the object calling them,
even though the method name remains the same.
Overloading allows creating multiple methods with the same name but different
parameters. Not directly supported in Python.
You can override methods from a parent class in the child class.
Example:
python
Copy code
class Bird:
def fly(self):
print("The bird flies.")
class Penguin(Bird):
def fly(self):
print("Penguins can't fly!")
1. Abstract Classes:
Abstract classes cannot be instantiated directly and must be inherited by subclasses.
Example:
python
Copy code
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
An interface defines a set of methods that must be implemented by any class that
"implements" the interface.
1. Composition:
python
Copy code
class Engine:
def start(self):
print("Engine started")
class Car:
def __init__(self):
self.engine = Engine()
def start_car(self):
self.engine.start()
2. Aggregation:
Similar to composition, but the lifecycle of the contained objects is not dependent on the
container.
python
Copy code
try:
x = int(input("Enter a number: "))
except ValueError:
print("That's not a valid number!")
python
Copy code
class NegativeValueError(Exception):
pass
def check_value(value):
if value < 0:
raise NegativeValueError("Value cannot be negative!")
python
Copy code
class FileHandler:
def write_to_file(self, filename, data):
with open(filename, 'w') as file:
file.write(data)
2. Serialization (Optional):
2. Multiple Inheritance:
Python supports multiple inheritance, though it’s not always recommended due to
complexity.
Example:
python
Copy code
class A:
pass
class B:
pass
Final projects typically involve integrating multiple OOP concepts like inheritance,
encapsulation, polymorphism, file handling, and error handling.
Example projects:
o Inventory System: Managing products with categories and stock levels.
o Library System: Managing books, borrowers, and due dates.