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

5.11.1. Overload Method

>>> class Parent:
...     def say_hello(self):
...         print('Parent says good morning')
>>>
>>>
>>> class Child(Parent):
...     def say_hello(self):
...         print('Child says wassup')
>>>
>>>
>>> obj = Child()
>>> obj.say_hello()
Child says wassup

5.11.2. Overload Attribute

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