5.6. OOP Inheritance Overload

5.6.1. Rationale

  • 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).

5.6.2. Overload Method

>>> class Parent:
...     def say_hello(self):
...         print('hello')
>>>
>>>
>>> class Child(Parent):
...     def say_hello(self):
...         print('yo')
>>>
>>>
>>> obj = Child()
>>> obj.say_hello()
yo

5.6.3. Overload Attribute

>>> class Parent:
...     def __init__(self):
...         self.firstname = 'Mark'
...         self.lastname = 'Watney'
>>>
>>>
>>> class Child(Parent):
...     def __init__(self):
...         self.job = 'astronaut'
>>>
>>>
>>> obj = Child()
>>> vars(obj)
{'job': 'astronaut'}

5.6.4. Assignments