Introduction:
Table of Contents
Python inheritance and composition are use in Object oriented programming for making relationship between class and objects. While you developing a application using object oriented programming it is important to understand the concept of composition.
In python composition are built particular striation dynamic. It is good idea to creating integration with class and objects.
In other word composition means to delegate the some responsibilities from one class to another class.
In the below example I make two classes one is salary class and 2nd is employee class. I delegate the responsibility to calculate monthly salary from salary class to employee class.
Example 1
# python 3.8
# python object oriented programming
# python class
# python module
# python inheritance and composition
class salary:
def __init__(self, basic_pay, bonus,overtime):
self.pay = basic_pay
self.bonus = bonus
self.overtime=overtime
def monthly_salary(self):
return (self.pay) + (self.bonus) +(self.overtime)
class employee:
def __init__(self, employee_name, position, age, basic_pay, bonus,overtime):
self.name = employee_name
self.position = position
self.age = age
self.pay = basic_pay
self.bonus = bonus
self.overtime=overtime
self.obj_salry = salary(basic_pay, bonus,overtime)
def total_salry(self):
return self.obj_salry.monthly_salary()
emp = employee('Sadaqat Hussain', 'Software Developer', 30, 120000, 12000, 5000)
print(emp.total_salry())
Output:
It is very difficult to understand the concept of oops in python and python inheritance with class composition. We clear the this another python inheritance and composition exercise . And in this exercise it will clear the concept of python inheritance super.
Example 2:
# python 3.8
# python object oriented programming
# python class
# python module
# python inheritance and composition
class price:
def __init__(self, cost_price, sale_price,tax):
self.cost = cost_price
self.retail = sale_price
self.tax=tax
def total_bill(self):
return (self.retail * 2) +(self.tax)
class product:
def __init__(self, product_name, cost_price, sale_price,tax):
self.pname = product_name
self.cost = cost_price
self.retail = sale_price
self.gst=tax
self.obj_price = price(cost_price, sale_price,tax)
def total_bill(self):
return self.obj_price.total_bill()
bill = product('hp probook i7', 85000, 90000,15300)
print('total bill :', bill.total_bill())
Output:
total bill : 195300
Use Aggregation In Python Instead of Composition
In above example we have discuss about python composition. Now in this example we will see how to use aggregation in python instead of composition with very basic example.
Python Aggregation Example:
# python 3.8
# python object oriented programming
# python class
# python module
# python inheritance and aggregation example
class salary:
def __init__(self, basic_pay, bonus,overtime):
self.pay = basic_pay
self.bonus = bonus
self.overtime=overtime
def monthly_salary(self):
return (self.pay) + (self.bonus) +(self.overtime)
class employee:
def __init__(self, employee_name, position, age, salary):
self.name = employee_name
self.position = position
self.age = age
self.obj_salry = salary;
def total_salry(self):
return self.obj_salry.monthly_salary()
salary=salary(20000,12000,5000)
emp = employee('Sadaqat Hussain', 'Software Developer', 30, salary)
print('Monthly Salary :',emp.total_salry())
Output:
Pingback: HOW TO CREATE ABSTRACT CLASSES IN PYTHON? | GSS TECHNOLOGY
Pingback: Free PHP MySQL POS With Source Code 2021 | GSS TECHNOLOGY