0% found this document useful (0 votes)
4 views20 pages

Lecture 4

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)
4 views20 pages

Lecture 4

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/ 20

YMCA COMPREHENSIVE INSTITUTE

Introduction to Programming

LESSON 4
OOP in Python

© YCI – 2025.
BIT 1203 2/22/2025
OOP
2

◻ What is OOP?
Object oriented programming
Computer programming model that organizes
software design around data, or objects.
OOP focuses on the objects that developers want
to manipulate rather than the logic required to
manipulate them.
The main concept of OOPs is to bind the data and
the functions that work on that together as a
single unit so that no other part of the code can
access this data.
© YCI – 2025. 2/22/2025
OOP:- Building blocks of OOP
3

◻ Classes are user-defined data types that act as the blueprint


for individual objects, attributes and methods.
Objects are instances of a class created with specifically defined
data.
■ Objects can correspond to real-world objects or an abstract entity.
Methods are functions that are defined inside a class that
describe the behaviors of an object.
■ Each method contained in class definitions starts with a reference to
an instance object.
■ Programmers use methods for reusability or keeping functionality
encapsulated inside one object at a time.
Attributes are defined in the class template and represent the
state of an object.
■ Objects will have data stored in the attributes field.
■ Class attributes belong to the class itself.

© YCI – 2025. 2/22/2025


OOP:- Building blocks …
4

© YCI – 2025. 2/22/2025


OOP:- Principles
5

◻ Encapsulation:
All important information is contained inside an object
and only select information is exposed
Implementation and state of each object are privately
held inside a defined class.
◻ Abstraction.
Objects only reveal internal mechanisms that are
relevant for the use of other objects, hiding any
unnecessary implementation code.
The derived class can have its functionality extended.
This concept can help developers more easily make
additional changes or additions over time.

© YCI – 2025. 2/22/2025


OOP:- Principles …
6

◻ Inheritance.
Relationships and subclasses between objects can be
assigned, enabling developers to reuse common logic
while still maintaining a unique hierarchy.
Allows defining parent and children classes.
◻ Polymorphism.
Objects are designed to share behaviors and they can take
on more than one form.
The program will determine which meaning or usage is
necessary for each execution of that object from a parent
class, reducing the need to duplicate code.
A child class is then created, which extends the
functionality of the parent class. Polymorphism allows
different types of objects to pass through the same
interface.

© YCI – 2025. 2/22/2025


Classes and objects in Python
7

◻ A piece of code from which you can generate a unique object, where each
object is a single instance of the class.
Think of it as a blueprint or factory from which you can create individual objects.
A class is a collection of objects
◻ Object:
One unit of data plus code generated from a class as an instance of that class.
◻ Attribute:
A characteristic of an object that contains information about the object.
Also called a property of the object.
An attribute name is preceded by dot, as in member.username which may
contain the username for one site member.
◻ Method/function:
A Python function that’s associated with the class.
It defines an action that object can perform.
In an object, you call a method by preceding the method name with a dot, and
following it with a pair of parentheses.
■ For example member.archive() may be a method that archives (deactivates) the
member’s account.

© YCI – 2025. 2/22/2025


Example of a class: Member class.
8

© YCI – 2025. 2/22/2025


Classes in Python …
9

◻ Some points on Python class:


Classes are created by keyword class.
Attributes are the variables that belong to a class.
Attributes are always public and can be accessed
using the dot (.) operator. Eg.: Myclass.Myattribute
◻ Class Definition Syntax:
class ClassName:
■ # Statement-1
■ .
■ .
■ .
■ # Statement-N

© YCI – 2025. 2/22/2025


Creating classes
10

◻ You create your own classes like you create


your own functions.
Legitimate class names start with a letter and
contain no spaces or punctuation
It’s customary to start a class name with an
uppercase letter to help distinguish classes from
variables
■ For example, to create a new class named Member,
use:
■ class Member:

© YCI – 2025. 2/22/2025


Empty classes
11

◻ Class that don’t contain any code in it.


◻ putting the keyword pass below the definition

© YCI – 2025. 2/22/2025


Objects in Python
12

◻ An object is an entity that has a state and


behavior associated with it:
State:
■ It is represented by the attributes of an object. It also
reflects the properties of an object.
Behavior:
■ It is represented by the methods of an object. It also
reflects the response of an object to other objects.
Identity:
■ It gives a unique name to an object and enables one
object to interact with other objects.

© YCI – 2025. 2/22/2025


Objects in Python ….
13

© YCI – 2025. 2/22/2025


Creating Objects in Python
14

◻ To grant to your class the ability to create


instances (objects) for you, you give the class
an init method
The word init is short for initialize
It’s really just a function that’s defined inside of a
class.
It must have the specific name __init__
■ That’s two underscores followed by init followed by
two more underscores.

© YCI – 2025. 2/22/2025


Creating Objects in Python …
15

◻ The syntax for creating an init method is:

def is short for define,


__init__ is the name of a built-in Python method
that’s capable of creating objects from within a
class.
self part is just a variable name, and it is used to
refer to the object being created at the moment.
Class member with two attributes uname and
fname

© YCI – 2025. 2/22/2025


Creating Objects in Python …
16

◻ Giving objects its attributes


The __init__ line creates a new empty object named self.
The self.username = uname line adds an attribute named
username to that empty object, and puts into that attribute
whatever is passed in as uname.
Then the self.fullname = fname line does the same thing
for the fullname attribute and the fname value that is
passed in

© YCI – 2025. 2/22/2025


Creating Objects in Python …
17

◻ Can create instances (objects) from it using


this simple syntax:
my_Object = ClassName(‘arg1’, ‘arg2’)
◻ For example
new_Member= Member(‘Mubs’, ‘Makerere Uni. Biz School’
◻ Print the object or its attributes
print(new_Member.username)
print(new_Member.fullname)
print(type(new_Member)

© YCI – 2025. 2/22/2025


Exercise
18

◻ Write a class based on the graphic below.


It should have the attributes and methods shown
in the graphic.
Create four car objects, print them on the
terminal.

© YCI – 2025. 2/22/2025


Exercise cont’d
19

◻ Write a class (about anything) with two functions in


addition to the __init__ function
The functions should include an if, else or a for loop,
or while loop
◻ Create a main function
In the main function create two object and use the
objects to call the functions created in the class.
◻ Label it as your_name_assignment3.py
E.g. Dawen_Ainebyoona_assignment3.py
◻ Upload .py file to
https://drive.google.com/drive/folders/1EbalPs0NiN6V08
HJC7h7xx_ZWfL-i5ql?usp=sharing

© YCI – 2025. 2/22/2025


Thank you

© YCI – 2025. 2/22/2025

You might also like