CS 121 - Object Oriented Programming (Lec 8)
CS 121 - Object Oriented Programming (Lec 8)
Object Oriented
Programming
Lecture 07
By: Syed Shahrooz Shamim
Object Oriented
Programming
Pillars of
Abstraction
OOP Encapsulation
Inheritance
Polymorphism
Inheritance
Pillars of OOP
• Create new classes (derived classes) from
Abstraction existing classes (base classes)
Inheritance SavingAccount
CustomerName : string
Account_type : string
Polymorphism Balance : int
Interest_rate : int
Deposit() : int
Withdrawal() : int
Calc_interest() : int
BankAccount
CustomerName : string
Pillars of OOP Account_type : string
Balance : int
Deposit() : int
Abstraction Withdrawal() : int
Encapsulation
Inheritance CheckingAccount SavingAccount
Insufficient_fund_fee : int Interest_rate : int
Polymorphism Process_deposit() : int Calc_interest() : int
How to Implement Inheritance
in Python
class A:
def __init__(self, n = ‘UIT'):
self.name = n
class B(A):
pass
object = B(“Usman”)
print (object.name)
Another Example of Inheritance
Create a Parent Class
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
x = Person(“Quaid", “Azam")
x.printname()
Another Example of Inheritance
Create a Child Class
class Student(Person):
pass
x = Student(“Allama", “Iqbal")
x.printname()
Add the __init__() Function
• So far we have created a child class that inherits the properties and
methods from its parent.
• When you add the __init__() function, the child class will no longer
inherit the parent's __init__() function.
• Now we have successfully added the __init__() function, and kept the
inheritance of the parent class, and we are ready to add functionality
in the __init__() function.
Use the super() Function
• Python also has a super() function that will make the child class inherit all
the methods and properties from its parent:
Example
class Student(Person):
def __init__(self, fname, lname):
super().__init__(fname, lname)
• By using the super() function, you do not have to use the name of the
parent element, it will automatically inherit the methods and properties
from its parent.
Add Properties
Example
Add a property called graduationyear to the Student class:
class Student(Person):
def __init__(self, fname, lname):
super().__init__(fname, lname)
self.graduationyear = 2021
• In the example below, the year 2019 should be a variable, and passed
into the Student class when creating student objects. To do so, add
another parameter in the __init__() function:
Add Properties
Example
Add a year parameter, and pass the correct year when creating objects:
class Student(Person):
def __init__(self, fname, lname, year):
super().__init__(fname, lname)
self.graduationyear = year
def welcome(self):
print("Welcome", self.firstname, self.lastname,
"to the class of", self.graduationyear)
Add Methods
Example
Add a method called welcome to the Student class:
Note: If you add a method in the child class with the same name as a
function in the parent class, the inheritance of the parent method will
be overridden.
Types of Inheritance
Example Multiple Inheritance Inheritance
class A:
# variable of class A
# functions of class A
class B:
# variable of class A
# functions of class A
class B(A):
# class B inheriting property of class A
# more properties of class B
class C(B):
# class C inheriting property of class B
# thus, class C also inherits properties of class A
# more properties of class C
Practice
# Deriving a child class from the two
parent classes
# Parent class 1
class TeamLeader(TeamMember, Worker):
class TeamMember(object):
def __init__(self, name, uid, pay,
def __init__(self, name, uid): jobtitle, exp):
self.name = name self.exp = exp
self.uid = uid TeamMember.__init__(self, name, uid)
Worker.__init__(self, pay, jobtitle)
# Parent class 2 print("Name: {}, Pay: {}, Exp:
class Worker(object): {}".format(self.name, self.pay, self.exp))
def __init__(self, pay, jobtitle):
self.pay = pay TL = TeamLeader(‘Ali', 10001, 250000,
self.jobtitle = jobtitle 'Scrum Master', 5)
Activity
• You are hired as a programmer your
task is to design an application for
class VLC Player. This Video Player
can Support multiple media formats.
Construct the objects and show.
• Now You need to add another Player
which supports only mp3 and mp2
formats.
• Your boss wants to make a new
player which can hold the properties
of both how can you do it?
• First make a UML class diagrams
what you understand from the above
then do Coding.