All technological notes.
Inheritance allows us to define a class that inherits all the methods and properties from another class.
Parent class / Base Class: the class being inherited from, also called base class.
Child class / Derived Class: the class that inherits from another class, also called derived class.
__init__()
__init__() function__init__() function overrides the inheritance of the parent’s __init__() function.super()
super() function returns an object that represents the parent class.print("\n--------Class: Inheritance--------\n")
class Person:
def __init__(self, fname, lname):
self.fname = fname
self.lname = lname
def __str__(self):
return f'{self.fname} {self.lname}'
def printName(self):
print(f"Name: {self.fname} {self.lname}")
# inherit Person Class
class Student(Person):
def __init__(self, fname, lname, id):
super().__init__(fname, lname)
self.id = id
def __str__(self):
return f"{super().__str__()} - {self.id}"
def printID(self):
print(f'ID: {self.id}')
std = Student("John", "Wick", "001")
print(std) # John Wick - 001
std.printName() # Name: John Wick
std.printID() # ID: 001