Object Oriented Programming(Python)
Emmanuel Ali(PhD)
April 27, 2026
Emmanuel Ali(PhD) 2nd Semester April 27, 2026 1 / 24
Outline
1 Object Oriented Programming
Emmanuel Ali(PhD) 2nd Semester April 27, 2026 2 / 24
Object Oriented Programming
Object oriented programming is a programming approach that uses the
concept of objects to represent data and code. An object is a data field
that has attribute and behaviour.
Object oriented programming enables to write neat and reusable code.
This helps with complex software design, easy maintenance, scalability
and efficiency.
Emmanuel Ali(PhD) 2nd Semester April 27, 2026 3 / 24
Object-Oriented Programming
Object-Oriented Programming (OOP) is a programming paradigm
based on the concept of "objects"
Objects can contain data and code
OOP focuses on:
Encapsulation
Inheritance
Polymorphism
Abstraction
Python fully supports OOP principles
Emmanuel Ali(PhD) 2nd Semester April 27, 2026 4 / 24
Classes, objects, methods and attributes
Class is a collection of objects. It act as the blueprint of objects,
attributes and methods.
Objects are instances of a class defined with specific data. They
tend to correspond to abstract entities or real world objects.
Methods describe the behaviour of the objects and are functions
within the class.
Attributes are the state of the object.
Emmanuel Ali(PhD) 2nd Semester April 27, 2026 5 / 24
Classes
Class is used to represent real-world things and situations. They define
the general behaviour a whole category of objects can have. Classes are
declared using the class statement and it statement generates a new
class object.
Each time a class is called, it generates is a new instance object and the
process is known as instantiation. Instances are automatically linked to
their classes and superclasses.
Emmanuel Ali(PhD) 2nd Semester April 27, 2026 6 / 24
Classes in Python
Classes are blueprints for creating objects
They define:
Attributes (data)
Methods (behaviors)
Created using the class keyword
Naming convention: CamelCase
Classes can inherit from other classes
Emmanuel Ali(PhD) 2nd Semester April 27, 2026 7 / 24
Classes - Basic Syntax
class ClassName :
""" Optional class documentation string """
# Class attributes
class_attribute = " I belong to the class "
# Constructor method
def __init__ ( self , param1 , param2 ) :
# Instance attributes
self . param1 = param1
self . param2 = param2
# Instance methods
def some_method ( self ) :
return " I 'm a method "
Emmanuel Ali(PhD) 2nd Semester April 27, 2026 8 / 24
Classes - Example
class Car :
""" A simple car class """
# Class attribute
wheels = 4
def __init__ ( self , make , model , year ) :
self . make = make
self . model = model
self . year = year
self . odometer = 0
def get_description ( self ) :
return f " { self . year } { self . make } { self . model } "
# Creating an instance
my_car = Car ( " Toyota " , " Corolla " , 2022)
Emmanuel Ali(PhD) 2nd Semester April 27, 2026 9 / 24
Empty Class
Empty class creates a class with nothing in it. The pass statement is
required as there is no method in it. Attributes can be attached later to
its instance.
class Dog () :
pass
Dog . name = " bingo "
Dog . age = 12
print ( Dog . name )
dog1 = Dog ()
dog2 = Dog ()
print ( dog1 . name , dog2 . name )
dog1 . name = " Jack "
print ( Dog . name , dog1 . name , dog2 . name )
Emmanuel Ali(PhD) 2nd Semester April 27, 2026 10 / 24
Attributes
Attributes are the state of the class and they are public. Attributes are
accessed using dot(.). It informs the program to find the first occurrence
of the attribute by looking in object, then in all classes above it, from
bottom to top and left to right.
There are two types of attributes namely: class and instance attributes
class Dog () :
def __init__ ( self , name , age ) :
self . name = name
self . age = age
dog = Dog ( Jack , 1)
print ( dog . name )
print ( dog . age )
Emmanuel Ali(PhD) 2nd Semester April 27, 2026 11 / 24
Attributes in Python
Attributes are variables that belong to classes or objects
Two types:
Class attributes - shared by all instances
Instance attributes - unique to each instance
Access using dot notation ([Link])
Can be added, modified, or deleted at runtime
Emmanuel Ali(PhD) 2nd Semester April 27, 2026 12 / 24
class Student :
# Class attribute
school = " Python University "
def __init__ ( self , name , student_id ) :
# Instance attributes
self . name = name
self . student_id = student_id
self . courses = []
# Creating instances
student1 = Student ( " Alice " , " A12345 " )
student2 = Student ( " Bob " , " B67890 " )
# Accessing attributes
print ( student1 . name ) # Alice
print ( student2 . name ) # Bob
print ( Student . school ) # Python University
print ( student1 . school ) # Python University
# Modifying attributes
student1 . courses . append ( " Python 101 " )
Student . school = " Python Academy " # Changes for all
instances
Emmanuel Ali(PhD) 2nd Semester April 27, 2026 13 / 24
Dynamic Attributes
class DynamicObject :
pass
obj = DynamicObject ()
# Adding attributes dynamically
obj . name = " Dynamic "
obj . value = 42
# Using __dict__ to see all attributes
print ( obj . __dict__ ) # { ' name ': ' Dynamic ', ' value ': 42}
# Deleting an attribute
del obj . value
print ( obj . __dict__ ) # { ' name ': ' Dynamic '}
Emmanuel Ali(PhD) 2nd Semester April 27, 2026 14 / 24
Methods in Python
Methods are functions defined inside a class
Types of methods:
Instance methods - operate on instance data
Class methods - operate on class data
Static methods - don’t operate on instance or class data
Instance methods take self as first parameter
Class methods take cls as first parameter with @classmethod
decorator
Static methods use @staticmethod decorator
Emmanuel Ali(PhD) 2nd Semester April 27, 2026 15 / 24
Methods - Instance Methods
class Rectangle :
def __init__ ( self , width , height ) :
self . width = width
self . height = height
# Instance method
def area ( self ) :
return self . width * self . height
# Instance method with parameters
def resize ( self , width , height ) :
self . width = width
self . height = height
return self . area ()
rect = Rectangle (10 , 5)
print ( rect . area () ) # 50
print ( rect . resize (8 , 3) ) # 24
Emmanuel Ali(PhD) 2nd Semester April 27, 2026 16 / 24
class MathUtils :
pi = 3.14159
def __init__ ( self , value ) :
self . value = value
# Instance method
def double ( self ) :
return self . value * 2
# Class method
@classmethod
def circle_area ( cls , radius ) :
return cls . pi * radius * radius
# Static method
@staticmethod
def add (a , b ) :
return a + b
# Using class and static methods
print ( MathUtils . circle_area (5) ) # 78.53975
print ( MathUtils . add (10 , 20) ) # 30
Emmanuel Ali(PhD) 2nd Semester April 27, 2026 17 / 24
Method
Method are a special type of functions that belong to a class. It can
similarly be called by the dot(.) notation, only difference with the
attributes there is a bracket in front of the method name.
class Dog () :
def __init__ ( self , name , age ) :
self . name = name
self . age = age
def move ( self ) :
print ( self . name . title () + " is moving . " )
dog = Dog ( " Jack " , 1)
dog . move ()
Emmanuel Ali(PhD) 2nd Semester April 27, 2026 18 / 24
Constructor
The __init__() method is the equivalent of constructors in C++ and
Java. It is set up at the initialization of the class, that means as soon as
the class is instantiated the __init__() method is called. If
__init__() is not present, the class call will return an empty instance.
The self statement is used to attach an attribute and method to a class.
It allows the attribute or method to access all round the class.
Emmanuel Ali(PhD) 2nd Semester April 27, 2026 19 / 24
Constructors in Python
Constructor is a special method called when an object is created
Defined using __init__() method
Used to:
Initialize instance attributes
Perform setup operations
Accept parameters for object creation
Always takes self as first parameter
Not required, but commonly used
Emmanuel Ali(PhD) 2nd Semester April 27, 2026 20 / 24
Constructor - Basic Example
class Person :
def __init__ ( self , name , age ) :
self . name = name
self . age = age
self . is_adult = age >= 18 # Derived attribute
# Some initialization work
print ( f " Creating a Person : { name } , { age } years old " )
def greet ( self ) :
return f " Hello , my name is { self . name } "
# Constructor called when object is created
person1 = Person ( " Alice " , 30)
person2 = Person ( " Bob " , 15)
print ( person1 . is_adult ) # True
print ( person2 . is_adult ) # False
Emmanuel Ali(PhD) 2nd Semester April 27, 2026 21 / 24
class BankAccount :
def __init__ ( self , owner , balance =0.0) :
self . owner = owner
self . balance = balance
def deposit ( self , amount ) :
self . balance += amount
return self . balance
def withdraw ( self , amount ) :
if amount <= self . balance :
self . balance -= amount
return True
return False
# Using default value
account1 = BankAccount ( " Alice " )
print ( account1 . balance ) # 0.0
# Overriding default value
account2 = BankAccount ( " Bob " , 100.0)
print ( account2 . balance ) # 100.0
Emmanuel Ali(PhD) 2nd Semester April 27, 2026 22 / 24
Exercise
Create a basic inventory system for a small store. You need to represent items in the
inventory with their name, price, and quantity. You should also be able to perform some
basic operations on these items.
Instructions:
1 Create a Class:
Define a Python class named Item.
2 Define the Constructor (__init__):
Takes name, price, and quantity as arguments.
Initializes them as attributes.
3 Display Item Details:
Create a method display_details().
Prints: "Item: Laptop, Price: $1200.00, Quantity: 5"
4 Update Quantity:
Create a method update_quantity(change).
Adds change to the current quantity.
5 Calculate Total Value:
Create a method calculate_total_value().
Returns: price * quantity.
Emmanuel Ali(PhD) 2nd Semester April 27, 2026 23 / 24
Develop a basic system for managing books in a library. Each book should have a title, an
author, a publication year, and an availability status. You also need to include some
operations for borrowing and returning books.
Instructions:
1 Create a Class:
Define a Python class named Book.
2 Define the Constructor (__init__):
Takes title, author, year, and available (a boolean) as
arguments.
Initializes them as attributes.
3 Display Book Info:
Create a method display_info().
Displays: "Title: ..., Author: ..., Year: ...,
Available: Yes/No".
4 Borrow Book:
Create a method borrow_book().
If the book is available, mark it as not available and return a
confirmation message.
Otherwise, return a message saying the book is already borrowed.
5 Return Book:
Create a method return_book().
Marks the book as available again.
Emmanuel Ali(PhD) 2nd Semester April 27, 2026 24 / 24