Skip to content

Commit 6ba2bfd

Browse files
authored
added composition program💻💻
1 parent 2ae7dff commit 6ba2bfd

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Beginner_Level/composition.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
# in composition we have delegates some responisibilities from subclass to superclass or vice versa if it don't have is-a relation for eg. employee and salary
3+
4+
class Salary:
5+
6+
def __init__(self, pay, bonus):
7+
self.pay = pay
8+
self.bonus = bonus
9+
10+
def annual_salary(self):
11+
return (self.pay*12) + self.bonus
12+
13+
class Employee:
14+
def __init__(self, name, age , pay, bonus):
15+
self.name= name
16+
self.age= age
17+
self.obj_salary = Salary(pay, bonus)
18+
19+
def total_salary(self):
20+
return self.obj_salary.annual_salary()
21+
22+
emp = Employee('rahul', 25 , 15000, 10000)
23+
24+
print(emp.total_salary())

0 commit comments

Comments
 (0)