3.10. OOP Inheritance Overload¶
Child inherits all fields and methods from parent
Used to avoid code duplication
- overload¶
When child has method or attribute with the same name as parent. In such case child attribute will be used (will overload parent).
3.10.1. Overload Method¶
>>> class Person:
... def say_hello(self):
... print('Hello')
>>>
>>>
>>> class Astronaut(Person):
... def say_hello(self):
... print('Howdy')
>>>
>>>
>>> mark = Astronaut()
>>> mark.say_hello()
Howdy
3.10.2. Overload Init¶
>>> class Person:
... def __init__(self):
... print('Person init')
>>>
>>>
>>> class Astronaut(Person):
... pass
>>>
>>>
>>> mark = Astronaut()
Person init
>>> class Person:
... def __init__(self):
... print('Person init')
>>>
>>>
>>> class Astronaut(Person):
... def __init__(self):
... print('Astronaut init')
>>>
>>>
>>> mark = Astronaut()
Astronaut init
3.10.3. Overload ClassVars¶
>>> class Person:
... firstname = 'Mark'
... lastname = 'Watney'
... job = None
>>>
>>>
>>> class Astronaut(Person):
... job = 'astronaut'
>>>
>>>
>>> mark = Astronaut()
>>>
>>> mark.firstname
'Mark'
>>> mark.lastname
'Watney'
>>> mark.job
'astronaut'
3.10.4. Overload Attribute¶
>>> class Person:
... def __init__(self):
... self.firstname = 'Mark'
... self.lastname = 'Watney'
... self.job = None
>>>
>>>
>>> class Astronaut(Person):
... def __init__(self):
... self.job = 'astronaut'
>>>
>>>
>>> mark = Astronaut()
>>> vars(mark)
{'job': 'astronaut'}