Section 1/4Calling the Parent Constructor
Calling the Parent Constructor
Explore this concept
super() and Protected Members
When a child class has its own __init__, it must explicitly call the parent's constructor using super().__init__(). Without this, the parent's instance variables are never set up.
Calling the Parent Constructor
class Person:
def __init__(self, name, age):
self._name = name
self._age = age
class Customer(Person):
def __init__(self, name, age, customer_id):
super().__init__(name, age) # run Person.__init__ first
self._customer_id = customer_idsuper()returns a reference to the parent class- Always call
super().__init__()as the first line of the child's__init__ - In Java the equivalent is
super(name, age);