0% found this document useful (0 votes)
15 views22 pages

Lecture One

Uploaded by

osesayjr
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)
15 views22 pages

Lecture One

Uploaded by

osesayjr
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/ 22

Limkokwing University of Creative Technology ⚙

Object Oriented Programming 1


Lecture 1: Introduction to Object Oriented Programming
Prepared By Alpha Omar Leigh (BSc, MSc)

What will You Learn after this Lecture.


In this lecture, you will learn:
1. The fundamental concepts of Object-Oriented Programming (OOP) in Python, focusing on the use of
classes and objects.
2. You will understand how to define classes, create objects, and utilize constructors to initialize attributes.
3. Additionally, you will explore methods to perform actions on objects, learn how to model real-world
entities like a Car and BankAccount, and grasp the importance of encapsulating data and behaviors
within classes for better code structure and reusability.

History of OOP
Object-Oriented Programming (OOP) originated in the 1960s to solve the complexity of large-scale software
development. The first programming language that fully embraced OOP concepts was Simula (developed in
Page 1 of 22
1967 by Ole-Johan Dahl and Kristen Nygaard). They introduced the idea of objects to model real-world entities
more intuitively.
Later, Smalltalk (developed in the 1970s by Alan Kay and his team) became one of the most influential OOP
languages, inspiring the development of modern OOP languages such as C++, Java, and Python.

The Problem OOP Solves


Before OOP, most programming was done using procedural programming , which relies on procedures or
functions to perform tasks. As software grew more complex, it became difficult to manage and maintain.
Procedural programming tends to focus on the steps to perform a task, which led to issues such as:
Code Duplication : Functions were often reused but had to be duplicated to handle similar but slightly
different tasks.
Complexity : As systems grew, managing and understanding the flow of data and processes became
harder.
Maintenance : Changes in one part of the code could lead to unpredictable results in another, making
software hard to maintain.
OOP was designed to solve these problems by organizing code around objects rather than actions. An object
represents a real-world entity, like a Car, Student, or Bank Account, and each object contains both data
(attributes) and methods (functions that act on the data).

Benefits of OOP
1. Modularity : Code is divided into objects, making it easier to manage and understand. If you need to
make a change, you can do so in one part of the system without affecting others.
2. Code Reusability : Through inheritance, you can create new objects that share the properties and
behaviors of existing ones, reducing duplication.
3. Flexibility and Scalability : OOP allows for easier updates and improvements to software systems. You
can add new features by creating new objects without rewriting existing code.
4. Encapsulation : Objects hide their internal state from the outside world and only expose certain
functions (methods), improving security and protecting data.
5. Abstraction : You can focus on higher-level designs without worrying about the complex details inside
each object.
6. Polymorphism : OOP allows one function to work in different ways depending on the object it is acting
on. For example, a method to draw a shape can behave differently when applied to a circle or a square.

Definition of Object-Oriented Programming (OOP)


Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects," which
can represent both real-world and abstract entities. In OOP, everything is structured around these objects, which
bundle data (attributes) and behavior (methods or functions) together.

Key Concepts of OOP:


1. Class : A blueprint for creating objects. A class defines the properties and behaviors that the objects
Page 2 of 22
created from it will have.
2. Object : An instance of a class. It contains data (attributes) and functions (methods) to perform
operations on the data.

3. Encapsulation : Bundling of data and methods that operate on the data within one unit (class) and
restricting access to some of the object's components.

4. Inheritance : A mechanism by which one class (child class) can inherit properties and behaviors from
another class (parent class), promoting code reuse.

Page 3 of 22
5. Polymorphism : The ability of different objects to respond to the same method call in different ways.

Difference Between OOP and Procedural Programming


Procedural programming (also known as imperative programming) is a paradigm based on procedures or
functions, which are blocks of code that perform a specific task. The program’s logic is built around functions,
where data is passed between them. In contrast, OOP organizes code around objects.

Page 4 of 22
Feature Object-Oriented Programming (OOP) Procedural Programming
Structure Based on objects and classes. Based on functions and procedures.
Data Handling Data and functions are encapsulated in Data is separate from functions, and
objects. passed between them.
Reusability Inheritance and polymorphism allow for Functions can be reused, but not as
code reuse. flexibly as in OOP.
Ease of Easier to maintain due to modular structure Harder to maintain as the program grows
Maintenance (classes). larger.
Abstraction Provides abstraction through objects, hiding Less abstraction; focus is on step-by-step
details. execution.
Real-World Excellent for modeling complex, real-world Less suited for complex, real-world
Modeling systems. modeling.
Example Creating a Car class and objects for each Writing separate functions for each part
car. of a car’s behavior.
Scalability More scalable for larger applications with Less scalable as complexity increases.
complex interactions.

Understanding Classes and Objects in Python


Python is an object-oriented programming language, meaning that it uses classes and objects to structure
code. These two concepts are essential building blocks for writing modular, scalable, and maintainable programs.

What is a Class and Object in Python?


Class:
A class is a blueprint for creating objects. It defines a structure that binds both data (attributes) and methods
Page 5 of 22
(functions) into a single unit.

Object :
An object is an instance of a class. It represents individual entities that have state (attributes) and behavior
(methods). Each object created from the same class will have unique values for its attributes but share the same
structure and behavior.

Example of Real-World Scenario:

Let’s say you want to model people in a software system:


The class could be Person .
The attributes (or states) of Person could be name , sex , and profession .
The methods (or behaviors) could be work() and study() .
Using this class, you can create multiple objects , each representing a specific individual with their own unique
states and behaviors.

Key Characteristics of an Object:


1. Identity : Every object must be uniquely identifiable in memory.
2. State : The current values of an object’s attributes.
3. Behavior : The actions the object can perform (defined by its methods).

Example of Objects:
Page 6 of 22
Let’s say we create two Person objects, Jessa and Jon .
Jessa :
Name: Jessa
Sex: Female
Profession: Software Engineer
Behavior: Working as a software developer, studying 2 hours a day.
Jon :
Name: Jon
Sex: Male
Profession: Doctor
Behavior: Working as a doctor, studying 5 hours a day.
Both Jessa and Jon are objects created from the same Person class, but they have different states
(attributes) and behaviors (methods).

Definition and Structure of a Class


What is a Class?
A class is a blueprint for creating objects. It defines the attributes (data) and methods (functions) that the objects
will have.
Syntax of a Class in Python:

class ClassName:
def __init__(self, attribute1, attribute2):
self.attribute1 = attribute1
self.attribute2 = attribute2

def method1(self):
# method implementation

def method2(self):
# method implementation

Components:
Page 7 of 22
class ClassName : Defines a new class.
__init__(self, attribute1, attribute2) : A special method called a constructor, which initializes
the object's attributes.
self.attribute1 = attribute1 : Assigns the value of the attribute to the object.
Methods: Functions defined within a class to represent behaviors.

Creating Objects (Instances) from a Class


What is an Object?
An object is an instance of a class. It represents a specific entity with attributes and behaviors defined by its class.
Syntax for Creating an Object:

object_name = ClassName(attribute1_value, attribute2_value)

Example: Creating a Simple Person Class


Class Definition:

class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def introduce(self):
return f"Hello, my name is {self.name} and I am {self.age} years old."

Creating an Object:

person1 = Person("Tindae", 30)


print(person1.introduce())

Constructors in Python
What is a Constructor?
A special method within a class that is automatically called when an object of that class is created.
Its purpose is to initialize the object's attributes (instance variables) and perform any necessary setup.
In Python, the constructor method is named __init__() .

Page 8 of 22
Object Creation and Initialization
Object creation involves allocating memory for the object.
Object initialization sets its initial state.
The __new__() method (which we don't explicitly define) handles memory allocation and returns the
newly created object.
The __init__() method initializes the object by setting its attributes.

Syntax of a Constructor
To create a constructor in Python, define a method named __init__() within your class.
The first parameter of __init__() is typically self , which refers to the current instance (object).
Inside the constructor, you can set initial values for the object's attributes.

def __init__(self):
# body of the constructor

Example: A Simple Class with a Constructor


class Book:
def __init__(self, title, author):
self.title = title
self.author = author

# Creating a Book object


my_book = Book(title="The Great Gatsby", author="F. Scott Fitzgerald")

The Role of self


self is a special word used inside class methods (like constructors or other instance methods).

Page 9 of 22
It refers to the current object that the method is working with.
Think of it as a way for the object to talk about itself.

Why Do We Need self ?


Imagine you're a book. You have a title, an author, and other details.
When someone interacts with you (like borrowing you), they need to know which book they're dealing
with.
self helps the book (object) keep track of its own information.

Example: A Talking Book (Class)


class Book:
def __init__(self, title, author):
self.title = title
self.author = author

Using self Inside Methods


When we call my_book.title , Python knows to look inside my_book (the current object) to find the
title attribute.
The self parameter in __init__() helps set these attributes for the specific book object.

Talking to Ourselves
Imagine the book saying, "Hey, I'm 'The Great Gatsby' by F. Scott Fitzgerald!"
self lets the book say that about itself.

Key Points to Remember


Constructors are automatically called when objects are created.
They initialize object attributes.
self is used to refer to the current object.
Constructors are essential for setting up objects properly.

Detailed Examples: Musician and Car Classes


1. Musician Class
1. UML Diagram for Musician Class

Page 10 of 22
+--------------------+
| Musician |
+--------------------+
| - name: str |
| - instrument: str |
| - band: str |
+--------------------+
| + play_instrument(): str |
| + introduce(): str |
+--------------------+

Class Definition:

class Musician:
def __init__(self, name, instrument, band):
self.name = name
self.instrument = instrument
self.band = band

def play_instrument(self):
return f"{self.name} is playing the {self.instrument}."

def introduce(self):
return f"{self.name} is a member of {self.band} and plays the {self.instrument}."

Creating Objects:

musician1 = Musician("Omar Leigh", "guitar", "The Beatles")


musician2 = Musician("Alusine Lavallie", "bass", "The Beatles")

print(musician1.play_instrument())
print(musician2.introduce())

2. Car Class
+--------------------+
| Car |
+--------------------+
| - make: str |
| - model: str |
| - year: int |
+--------------------+
| + start(): str |
| + stop(): str |
| + honk(): str |
+--------------------+

Class Definition:

Page 11 of 22
class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year

def start(self):
return f"The {self.year} {self.make} {self.model} is starting."

def stop(self):
return f"The {self.year} {self.make} {self.model} is stopping."

def honk(self):
return "Honk! Honk!"

Creating Objects:

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


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

print(car1.start())
print(car2.honk())

Example: Creating a Person Class


Let’s create a class called Person with attributes like name , sex , and profession , and methods to show a
person’s details and define their work behavior.

class Person:
def __init__(self, name, sex, profession):
# Data members (instance variables)
self.name = name
self.sex = sex
self.profession = profession

# Behavior (instance method)


def show(self):
print(f'Name: {self.name}, Sex: {self.sex}, Profession: {self.profession}')

# Behavior (instance method)


def work(self):
print(f'{self.name} is working as a {self.profession}')

Explanation:
__init__ method : This is a constructor method in Python, automatically called when you create a new
object. It initializes the attributes of the object.
Instance Variables : self.name , self.sex , self.profession represent the state of an object.
Instance Methods : show() and work() define the behavior of the object.

Page 12 of 22
Creating Objects of a Class
Now, let’s create objects from the Person class.

# Creating objects
person1 = Person("Fatu", "Female", "Software Engineer")
person2 = Person("Hassan", "Male", "Doctor")

# Accessing methods
person1.show()
person1.work()

person2.show()
person2.work()

Output:
Name: Fatu, Sex: Female, Profession: Software Engineer
Fatu is working as a Software Engineer
Name: Hassan, Sex: Male, Profession: Doctor
Hassan is working as a Doctor

Here, person1 and person2 are two different objects of the Person class, each with its own values for name ,
sex , and profession .

Attributes in Classes
Understanding Attributes
Attributes are like the characteristics or properties of an object. They define what an object is and what it can
do. There are two main types:
1. Instance Attributes:
Belong to individual objects.
Defined within the __init__ method.
Accessed using self.attribute_name .
2. Class Attributes:
Shared by all instances of a class.
Defined directly within the class.
Accessed using ClassName.attribute_name or self.attribute_name .

Page 13 of 22
Example: The Employee Class
Let's use an Employee class to illustrate these concepts:

class Employee:
# Class attribute
company_name = "Limkokwing University"

def __init__(self, name, salary):


# Instance attributes
self.name = name
self.salary = salary

# Creating two employee objects


employee1 = Employee("Omar Leigh", 50000)
employee2 = Employee("Jane Smith", 60000)

In this example:
company_name is a class attribute . It's shared by all employees in the company.
name and salary are instance attributes . Each employee has their own unique name and salary.

Accessing Attributes
Instance attributes:

Page 14 of 22
employee1.name # Accessing the name of employee1
employee2.salary # Accessing the salary of employee2

Class attributes:

Employee.company_name # Accessing the company name directly


employee1.company_name # Accessing the company name through the instance

Why Use Attributes?


Encapsulation: Attributes help encapsulate the data and behavior of an object, making it more
modular and easier to manage.
Flexibility: You can create objects with different values for their instance attributes, allowing for
customization.
Readability: Well-defined attributes improve code readability and understanding.

Class and Instance Methods: The Employee Example


Understanding Methods
Methods are functions defined within a class. They define the behaviors or actions that objects of that class can
perform. There are two main types:
1. Instance Methods:
Belong to individual objects.
Take self as the first parameter.
Can access both instance and class attributes.
2. Class Methods:
Belong to the class itself.
Decorated with the @classmethod decorator.
Take cls as the first parameter.
Can only access class attributes.

Example: The Employee Class (Continued)

Page 15 of 22
class Employee:
company_name = "Limkokwing University"

def __init__(self, name, salary):


self.name = name
self.salary = salary

# Instance method
def get_salary(self):
return f"{self.name} earns {self.salary}"

# Class method
@classmethod
def company_info(cls):
return f"Company: {cls.company_name}"

# Creating employee objects


employee1 = Employee("Omar Leigh", 50000)
employee2 = Employee("Alusine Lavallie", 60000)

Using Methods
Instance methods:
employee1.get_salary() # Output: "Omar Leigh earns 50000"

Class methods:

Employee.company_info() # Output: "Company: Limkokwing University"

Why Use Methods?


Encapsulation: Methods help encapsulate the behavior of an object within the class, making it more
modular and easier to maintain.
Reusability: You can define methods that can be used by multiple objects of the same class, promoting
code reusability.
Abstraction: Methods allow you to hide the implementation details of an object's behavior, providing a
simpler interface for users.

Key Points to Remember


Instance methods operate on individual objects.
Class methods operate on the class itself.
self is used to refer to the current object in instance methods.
cls is used to refer to the class itself in class methods.
Choose the appropriate method type based on whether the behavior is specific to individual objects or
shared by all instances.

Problem Statement: Student Class Implementation


Page 16 of 22
You are tasked with creating a Python program that defines a Student class. The class should represent the
essential properties of a student and their academic progress. Each student will have personal details, grades for
different subjects, and methods to display their information and academic performance.

Requirements:
1. Attributes :
Class attribute:
school : A class attribute to store the name of the school that all students belong to.
Instance attributes:
name : The name of the student.
age : The age of the student.
student_id : A unique ID for each student.
subject : The subject the student is majoring in.
grades : The grade the student has received for different assessments (e.g., exam,
project).
2. Methods :
__init__(self, name, age, student_id, subject, exam_grade, project_grade) : A
constructor to initialize the student's attributes.
display_info(self) : A method to display the student's name, age, student ID, subject, and
grades.
calculate_average(self) : A method to calculate and display the student's average grade.
update_grade(self, exam_grade=None, project_grade=None) : A method to update the
student's exam or project grade.

Solution: Python Code

Page 17 of 22
# Define the Student class
class Student:
# Class attribute (common to all students)
school = "Green Valley High School"

# Constructor (used to initialize instance attributes)


def __init__(self, name, age, student_id, subject, exam_grade, project_grade):
self.name = name # Instance attribute for student's name
self.age = age # Instance attribute for student's age
self.student_id = student_id # Instance attribute for student ID
self.subject = subject # Instance attribute for subject
self.exam_grade = exam_grade # Instance attribute for exam grade
self.project_grade = project_grade # Instance attribute for project grade

# Method to display student's information


def display_info(self):
print(f"Student Name: {self.name}")
print(f"Age: {self.age}")
print(f"Student ID: {self.student_id}")
print(f"Subject: {self.subject}")
print(f"Exam Grade: {self.exam_grade}")
print(f"Project Grade: {self.project_grade}")
print(f"School: {Student.school}") # Access class attribute

# Method to calculate the average grade


def calculate_average(self):
average = (self.exam_grade + self.project_grade) / 2
print(f"Average Grade for {self.name}: {average}")

# Method to update the exam or project grade


def update_grade(self, exam_grade=None, project_grade=None):
if exam_grade is not None:
self.exam_grade = exam_grade
if project_grade is not None:
self.project_grade = project_grade
print(f"Grades updated for {self.name}.")
self.display_info() # Display updated information

# Create instances (objects) of the Student class


student1 = Student("Alusine", 16, 101, "Mathematics", 85, 90)
student2 = Student("Musa", 17, 102, "Physics", 78, 88)

# Display student information


print("---- Student 1 Information ----")
student1.display_info()
student1.calculate_average()

print("\n---- Student 2 Information ----")


student2.display_info()
student2.calculate_average()

# Update grades for student1


print("\n---- Updating Grades for Student 1 ----")
student1.update_grade(exam_grade=90)

Explanation:
Page 18 of 22
1. Class and Instance Attributes:
The class attribute school is shared across all student objects, meaning every student belongs to the
same school.
Instance attributes ( name , age , student_id , subject , exam_grade , and project_grade ) are
unique for each student and are initialized through the __init__ constructor.

2. Methods:
display_info() : This method prints out the personal and academic details of a student.
calculate_average() : This method calculates the average of the exam and project grades and
displays it.
update_grade() : This method updates a student’s grades for either the exam, the project, or both. If
you provide a value for exam_grade , it will update that specific grade. If you provide a value for
project_grade , it updates that. If no new values are provided, the grades remain unchanged.

3. Creating and Using Objects:


We create two student objects: student1 and student2 , using their respective details.
Then we call the methods to display their information, calculate their average grades, and finally update
the grades of student1 .

Challenge To Solve in Class : Problem Statement


You are tasked with creating a Python class named Employee to represent employees in a company. This class
should encapsulate various attributes and methods to manage employee data effectively. Your goal is to
implement the following features:

Class Attributes:
1. company_name (string) - The name of the company.
2. total_employees (integer) - A counter to keep track of the total number of employees.

Instance Attributes:
1. name (string) - The employee's name.
2. employee_id (integer) - A unique identifier for the employee.
3. department (string) - The department where the employee works.
4. salary (float) - The employee's salary.
5. hire_date (datetime object) - The date the employee was hired.
6. is_active (boolean) - Indicates whether the employee is currently active.
7. position (string) - The employee's job title.
8. email (string) - The employee's email address.
9. hourly_rate (float) - The employee's hourly wage.

Page 19 of 22
10. university (string) - The name of the university the employee attended.

Instance Methods:
1. get_employee_info() - Returns a string containing the employee's name, ID, department, salary, hire
date, position, email, university, and whether they are active.
2. calculate_annual_salary() - Calculates the employee's annual salary based on their hourly rate and
a standard 40-hour work week.
3. get_years_of_service() - Calculates the number of years the employee has been with the company.
4. terminate_employment() - Sets the is_active attribute to False .
5. update_email(new_email) - Updates the employee's email address.
6. promote(new_position, salary_increase) - Updates the employee's position and increases their
salary by the specified amount.
7. is_eligible_for_retirement() - Returns True if the employee has been with the company for 20
years or more, otherwise False .
8. update_university(new_university) - Updates the employee's university name.

Class Methods:
1. get_company_name() - Returns the name of the company.
2. get_total_employees() - Returns the total number of employees.

Challenge:
Implement the Employee class with the above attributes and methods. Ensure that the class methods and
instance methods work correctly and that the class attributes are properly managed. Test your implementation
with various employee instances to verify that all functionalities are working as expected.

Homework Assessment 1: Car Class Implementation


Classwork Problem Statement:
You are required to design a Python class named Car to represent cars in a dealership system. This class should
include various attributes and methods to manage car information effectively. Your task is to implement the
following features:

Class Attributes:
1. dealership_name (string) - The name of the dealership.
2. total_cars (integer) - A counter to keep track of the total number of cars in the dealership.

Instance Attributes:
1. make (string) - The manufacturer of the car.
2. model (string) - The model of the car.
Page 20 of 22
3. year (integer) - The year the car was manufactured.
4. vin (string) - The Vehicle Identification Number of the car.
5. price (float) - The price of the car.
6. is_sold (boolean) - Indicates whether the car is sold.
7. mileage (integer) - The mileage of the car.
8. color (string) - The color of the car.

Instance Methods:
1. get_car_info() - Returns a string containing the car's make, model, year, VIN, price, mileage, color,
and whether it is sold.
2. sell_car() - Sets the is_sold attribute to True .
3. update_price(new_price) - Updates the car's price.
4. is_available() - Returns True if the car is not sold, otherwise False .
5. update_mileage(new_mileage) - Updates the car's mileage.

Class Methods:
1. get_dealership_name() - Returns the name of the dealership.
2. get_total_cars() - Returns the total number of cars in the dealership.

Challenge:
Implement the Car class with the above attributes and methods. Ensure that the class methods and instance
methods work correctly and that the class attributes are properly managed. Test your implementation with
various car instances to verify that all functionalities are working as expected.

Homework Assessment 2: BankAccount Class Implementation


Classwork Problem Statement:
You are required to design a Python class named BankAccount to represent bank accounts in a banking system.
This class should include various attributes and methods to manage account information effectively. Your task is
to implement the following features:

Class Attributes:
1. bank_name (string) - The name of the bank.
2. total_accounts (integer) - A counter to keep track of the total number of bank accounts.

Instance Attributes:
1. account_holder (string) - The name of the account holder.
2. account_number (string) - The unique account number.
Page 21 of 22
3. balance (float) - The current balance of the account.
4. account_type (string) - The type of the account (e.g., savings, checking).
5. is_active (boolean) - Indicates whether the account is active.
6. interest_rate (float) - The interest rate applicable to the account.

Instance Methods:
1. get_account_info() - Returns a string containing the account holder's name, account number,
balance, account type, and whether it is active.
2. deposit(amount) - Adds the specified amount to the account balance.
3. withdraw(amount) - Subtracts the specified amount from the account balance if sufficient funds are
available.
4. calculate_interest() - Calculates the interest earned based on the current balance and interest rate.
5. close_account() - Sets the is_active attribute to False .

Class Methods:
1. get_bank_name() - Returns the name of the bank.
2. get_total_accounts() - Returns the total number of bank accounts.

Challenge:
Implement the BankAccount class with the above attributes and methods. Ensure that the class methods and
instance methods work correctly and that the class attributes are properly managed. Test your implementation
with various account instances to verify that all functionalities are working as expected.

Page 22 of 22

You might also like