So far we have created a child class that inherits the properties and methods from its parent.
We want to add the __init__()
function to the child class (instead of the pass
keyword).
Note: The __init__()
function is called automatically every time the class is being used to create a new object.
Add the __init__()
function to the Student
class:
class Student(Person):
def __init__(self, fname, lname):
#add properties etc.
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:
class Student(Person):
def __init__(self, fname, lname):
Person.__init__(self, fname, lname)
Now we have successfully added the __init__() function, and kept the inheritance of the parent class, and we are ready to add functionality in the __init__()
function.
Track your progress - it's free!
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4