All courses › Intermediate Programming › Inheritance and super()
Inheritance and super()
A class can inherit from another: class Hund(Dyr): makes Hund a subclass of Dyr and gives it access to the parent class's methods. super().__init__() calls the parent's constructor, so the attributes from there are set up correctly.
inherits all methods and attributes from
calls the constructor of the parent class
Symbols
| the parent class (superclass) | ||
| the subclass |
Example
class Hund(Dyr):
def __init__(self, navn): super().__init__(navn)
All attributes from Dyr are set up automatically.
Forget
Practise object orientation for free →
super().__init__() and the subclass will be missing the attributes from the parent class.