-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path#19-inheritance.py
37 lines (26 loc) · 1.03 KB
/
#19-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
#Inheritance in python
# Parent Class is being inherited from a Base class(python Documentation)
class ParentClass:
def __init__(self, fname, lname):
self.fname = fname
self.lname = lname
def fullName(self):
print("My Name is " + self.fname + " " + self.lname)
# object created from Parent class
myParent = ParentClass('Sukumar', 'Mandal')
# Child class inherited from Parent class pass parent class as a parameter
class Child(ParentClass):
# when we use init we override the parent value
def __init__(self, fname, lname, year, sub):
# to keep the class to inherit from parent class add a call to super method
super().__init__(fname, lname)
self.GYear = year
self.subject = sub
#object Method for Child class only
def comeMessage(self):
print("your grd year is", self.GYear)
iamChild = Child("jhon", 'doe', 2019, 'Biology')
iamChild.comeMessage()
#we can also access the parent object method and varriable
print(iamChild.fname)
iamChild.fullName()