0% found this document useful (0 votes)
6 views10 pages

Object-Oriented Programming in Pythonv2

The document provides an introduction to Object-Oriented Programming (OOP) in Python, covering fundamental concepts such as classes, objects, inheritance, polymorphism, abstraction, and encapsulation. It explains how to create classes and objects, define attributes and behaviors, and highlights the benefits of OOP like code reusability and simplified maintenance. Additionally, the document includes hands-on exercises to practice creating classes and implementing OOP principles.

Uploaded by

mukisasamuel2020
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)
6 views10 pages

Object-Oriented Programming in Pythonv2

The document provides an introduction to Object-Oriented Programming (OOP) in Python, covering fundamental concepts such as classes, objects, inheritance, polymorphism, abstraction, and encapsulation. It explains how to create classes and objects, define attributes and behaviors, and highlights the benefits of OOP like code reusability and simplified maintenance. Additionally, the document includes hands-on exercises to practice creating classes and implementing OOP principles.

Uploaded by

mukisasamuel2020
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/ 10

10/8/23

OBJECT-ORIENTED PROGRAMMING
IN PYTHON

INTRODUCTION TO PYTHON &


VIRTUAL ENVIRONMENT
WHAT ARE WE GOING TO LEARN TODAY?

Ø Fundamental of OOP
o Classes
o Objects
o Creating Class & Objects
Ø Main Concepts of OOP
o Inheritance
o Polymorphism
o Abstraction
o Encapsulation
o Composition

1
10/8/23

FUNDAMENTAL
CONCEPT
OF OOP

INTRODUCTION TO OOP

Object 1 Object 2
Main Program

Attributes Attributes

Methods Methods
Function 1 Function 2 Function 3

Object 3
Function 4 Function 5

Attributes

Function 6 Methods

● Procedural Programming ● Object-Oriented Programming is a programming paradigm


● Creating functions that designs a program with the use of interacting objects.
● As program grow by adding more features, program are ● Benefits of OOP:
getting more complex ○ simplifies development and maintenance
● Hard to maintain, debug and reuse code ○ adaptable/flexible programs
○ code reusability

2
10/8/23

WHAT IS OBJECT IN OBJECT ORIENTED PROGRAMMING?

1. One of the popular approach in using


OOP is creating an objects
2. Object has two characteristics
● Attributes/Data
● Methods/Behaviour
3. The concept of OOP in Python focuses
on creating reusable code (Don’t ATTRIBUTES/DATA METHODS/BEHAVIOUR
• Name • Singing
Repeat Yourself) • Age • Dancing
4. To reuse the code we need to have • Color
object template à Creating a Class
OBJECT
Parrot

WHAT IS CLASS
OBJECT OBJECT
Parrot 1 Parrot 2

Class
Blueprint/template for the object

ATTRIBUTES/DATA METHODS/BEHAVIOUR ATTRIBUTES/DATA METHODS/BEHAVIOUR


• Name: Jane • Singing • Name: Blu • Singing
• Age: 1 • Dancing • Age: 2 • Dancing
• Color: Red • Color: Blue

3
10/8/23

HOW TO CREATE CLASS AND OBJECT?


1. To create a template/blueprint of an object, use “Class” in the program
Example:
The example for class of parrot can be

Object

3. To create multiple object, call class name with different variables name
Example:

HOW TO CREATE OBJECT ATTRIBUTES &


BEHAVIOUR?
1. From previous example, we have created class with the name “Parrot”. Inside object we need to define
what is the attributes & behaviour.
2. These attributes are defined inside the __init__ method of the class. Every time we create new object
__init__ will run first.
3. By using Parrot example, we can create instance attributes for multiple object from the same class.

4
10/8/23

HOW TO CREATE OBJECT ATTRIBUTES &


BEHAVIOUR?
4. After creating attributes, we need to defined
what is the behaviours of an object.
5. To create behaviours of an object, instance
behaviours/method must be defined inside our
Class template
6. By taking Parrot previous example, we know
that behaviours of Parrot is “sing” and “dance”
7. Create instance behaviours in our class
template

MAIN CONCEPT
OF OOP

10

5
10/8/23

INHERITANCE
● Inheritance is the capability of one class to derive or inherit the properties from another class
● The class that derives properties is called the derived class or child class and the class from which the

properties are being derived is called the base class or parent class
● Benefit:

i. Represents real-world relationships well


ii. Provides reusability of a code. No need to write the same code again and again
iii. Transitive in nature, which means that if class B inherit from another class A, then all the subclasses
of B would automatically inherit from Class A

11

EXAMPLE INHERITANCE

● We create 2 classes i.e. Bird (parent class) & Penguin (child


class)
● The child class inherit the function from parent class. We can
see from swim() method
● The child class (Penguin) modified the behaviour of the
parent class (Bird) à whoisThis() method
● We extend functions of the parent class by creating a new
run() method
● Super() is used to run __init__ method of parent class inside
the child class

12

6
10/8/23

POLYMORPHISM

● A child class inherits all the methods from the parent class
● In some situations, the method inherited from the parent class doesn’t quite fit into the child class

● Need to re-implement method in the child class

● Different methods to use polymorphism in Python:


a. Polymorphism with function and objects

b. Polymorphism with class methods

c. Polymorphism with inheritance

13

EXAMPLE POLYMORPHISM

● We defined 2 classes “Parrot” and “Penguin”


● Each of the class have common method à fly()
method with different functions
● To use polymorphism, create “common interface”
function that takes any object and call the object’s
fly() method
● When pass “blu” and “peggy” object in common
interface function, it can ran effectively

14

7
10/8/23

ABSTRACTION
● Abstraction is the act of dealing with ideas rather than actual objects or events.
● In OOP, this refers to the act of hiding implementation details from users and only showing them the

important things – they will know what the functionalities of an object are but not the how.
● Benefits of abstraction:

○ Reduces code complexity


○ Reduces the need to duplicate codes

○ Increases security by only providing important and relevant information to users

Encapsulation Abstraction

Data/information hiding Detail/implementation hiding

Provides an interface for users


Groups together class
that hides details of
members (data and methods)
implementation

15

ENCAPSULATION
● Encapsulation is one of the fundamental concepts in OOP
● It describes the idea of wrapping data and the methods that work on data within one unit
● Puts restrictions on accessing variables and methods directly and can prevent the accidental modification of data
● To prevent accidental change, an object’s variable can only be changes by an object’s method
● Those variables are known as private variables
● A class is an example of encapsulation as it encapsulates all the data that is member functions, variables etc

16

8
10/8/23

EXAMPLE: ENCAPSULATION

● Defined a “computer” class


● Use __init__ to store the maximum
selling price of computer
● Modify the value of __maxprice outside
the class
● Since __maxprice is a private variable,
the modification is not seen on the
output
● To change the value, use setter function
that take price as parameter

17

HANDS ON

18

9
10/8/23

HANDS ON (EXERCISE)

1. Write a Python program to create a Vehicle class with max_speed and mileage instance attributes
2. Create a vehicle class without any variables and methods

3. Create a child class Bus that will inherit all of the variables and method of the Vehicle class

4. Create a Bus class that inherits from the Vehicle class. Give the capacity argument of
Bus.seating_capacity() a default value of 50
5. Define a class attribute “color” with a default value “white”

https://pynative.com/python-object-oriented-programming-oop-exercise/#h-
oop-exercise-1-create-a-class-with-instance-attributes

19

THANK YOU

20

10

You might also like