-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path60_Python_Inheritance.py
100 lines (59 loc) · 1.62 KB
/
60_Python_Inheritance.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Python Inheritance
print("Python Inheritance:\n")
# Creating a parent class
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
# Creating an object using the Person class
object1 = Person('MD', 'Zaber')
# Executing the printname method
object1.printname()
print("\n")
# Creating a child class
class Student(Person):
pass
# Creating an object from the child class
student1 = Student('John', 'Doe')
student2 = Student('Mike', 'Olsen')
# Executing printname method
student1.printname()
student2.printname()
print("\n")
# Adding the __init__() Function
class Army(Person):
def __init__(self, fname, lname):
Person.__init__(self, fname, lname)
army1 = Army('Jack', 'Dalton')
army2 = Army('Dale', 'Barbie')
army1.printname()
army2.printname()
print("\n")
# Using the super() Function
class Engineer(Person):
def __init__(self, fname, lname):
super().__init__(fname, lname)
engineer1 = Engineer('Saad', 'Araf')
engineer1.printname()
print("\n")
# Adding Properties i.e serial
class Instructor(Person):
def __init__(self, fname, lname, serial):
super().__init__(fname, lname)
self.serial = serial
instructor1 = Instructor('Abdur', 'Rahman', 5)
instructor1.printname()
print(instructor1.serial)
print("\n")
# Adding Methods
class Driver(Person):
def __init__(self, fname, lname):
super().__init__(fname, lname)
def welcome(self):
print('Welcome to', self.firstname, self.lastname, 'in our company!')
d1 = Driver('Chanchal', 'Biswas')
d1.printname()
d1.welcome()
print("\n")