Object-Oriented Programming in Pythonv2
Object-Oriented Programming in Pythonv2
OBJECT-ORIENTED PROGRAMMING
IN PYTHON
Ø 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
2
10/8/23
WHAT IS CLASS
OBJECT OBJECT
Parrot 1 Parrot 2
Class
Blueprint/template for the object
3
10/8/23
Object
3. To create multiple object, call class name with different variables name
Example:
4
10/8/23
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:
11
EXAMPLE INHERITANCE
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
13
EXAMPLE POLYMORPHISM
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:
Encapsulation Abstraction
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
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