-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path44.inheritance2.py
More file actions
35 lines (31 loc) · 1.25 KB
/
44.inheritance2.py
File metadata and controls
35 lines (31 loc) · 1.25 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
# 44. Inheritance 2
"""
# Python also have a super() function that will make the child class inherit all the methods and
properties from its parent
# By using the super() function, you do not have to use the name of the parent element, it will
automatically inherit the methods and properties from its parent.
# If you add a method in the child class with the same name as a function in the parent class,
the inheritance of the parent method will be overridden.
"""
class Person():
def __init__(s, fname, lname):
s.firstName = fname
s.lastName = lname
def printName(self):
print(self.firstName, self.lastName)
def printInfo(self):
print(self.firstName, self.lastName,self.age)
p = Person("Omar", "Abdulaziz")
p.printName()
class Student(Person):
def __init__(s, fname, lname, a):
#Person.__init__(s, fname, lname)
super().__init__(fname, lname)
#s.age = 23
s.age = a
def printInfo(self): # Override the method in the parent class..
print("Name:", self.firstName,self.lastName , ", Age:", self.age)
#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", 24)
p1.printInfo()