-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiple_inheritence_example1.py
More file actions
64 lines (50 loc) · 1.58 KB
/
Copy pathmultiple_inheritence_example1.py
File metadata and controls
64 lines (50 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# ------------------ CLASS STRUCTURE (Multiple Inheritance) ------------------
#
# Person Job
# | |
# | |
# -----> Employee <-----
#
# Employee inherits properties from both Person and Job
# ---------------------------------------------------------------------------
# Parent Class 1
class Person:
# Constructor to initialize name
def __init__(self, name):
self.name = name # store the name in the object
# Parent Class 2
class Job:
# Constructor to initialize salary
def __init__(self, salary):
self.salary = salary # store the salary in the object
# Child Class inheriting from both Person and Job
class Employee(Person, Job):
# Constructor of Employee
def __init__(self, name, salary):
# Call constructor of Person to set name
Person.__init__(self, name)
# Call constructor of Job to set salary
Job.__init__(self, salary)
# Method to display employee details
def details(self):
print(self.name, "earns", self.salary)
# ------------------ PROGRAM FLOW ------------------
#
# emp = Employee("Jennifer", 50000)
# |
# |-- Person.__init__() → sets name
# |
# |-- Job.__init__() → sets salary
# |
# v
# emp.details() → prints name and salary
#
# Object Memory:
# emp
# ├── name = "Jennifer"
# └── salary = 50000
# --------------------------------------------------
# Create an object of Employee
emp = Employee("Jennifer", 50000)
# Call the method to display details
emp.details()