-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path43.inheritance.py
More file actions
32 lines (28 loc) · 1.1 KB
/
43.inheritance.py
File metadata and controls
32 lines (28 loc) · 1.1 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
# 43. Inheritance
"""
# Inheritance allows us to define a class that inherits all the methods and properties from
another class.
# To create a class that inherits the functionality from another class, send the parent class as a
parameter when creating the child class.
# When you add the __init__() function, the child class will no longer inherit the parent's
__init__() function.
# Note: The child's __init__() function overrides the inheritance of the
parent's __init__() function.
# To keep the inheritance of the parent's __init__() function, add a call to the parent's
__init__() function: parent.__init__(...,...,..)
"""
class Person():
def __init__(s, fname, lname):
s.firstName = fname
s.lastName = lname
def printName(self):
print(self.firstName, self.lastName)
p = Person("Omar", "Abdulaziz")
p.printName()
class Student(Person):
def __init__(s, fname, lname):
Person.__init__(s, fname, lname)
#Note: Use the pass keyword when you do not want to add any other properties or methods to the class.
#pass
p1 = Student("Ali", "Hassan")
p1.printName()