16.21. OOP Inheritance Overload

  • Child inherits all fields and methods from parent

  • Used to avoid code duplication

  • When child has method or attribute with the same name as parent

  • In such case child attribute will be used (will overload parent)

overload

When child has method or attribute with the same name as parent. In such case child attribute will be used (will overload parent).

16.21.1. 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

16.21.2. Overload Attribute

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

16.21.3. Assignments

"""
* Assignment: OOP Override Method
* Type: class assignment
* Complexity: easy
* Lines of code: 2 lines
* Time: 2 min

English:
    1. Use class `User`
    2. Modify class `Admin` to display `Admin login` in `login()` method
    3. Do not change inheritance
    4. Run doctests - all must succeed

Polish:
    1. Użyj klasy `User`
    2. Zmodyfikuj klasę `Admin` tak aby w metodzie `login()` wyświetlała `Admin login`
    3. Nie zmieniaj dziedziczenia
    4. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> mark = User('Mark', 'Watney')
    >>> mark.show()
    'Watney, Ares 3'
    >>> melissa = User('Lewis', 'Ares 3')
    >>> melissa.show()
    'Lewis, Ares 3'
"""

class User:
    def login(self):
        print('User login')


# Modify class `Admin` to display `Admin login` in `login()` method
# Do not change inheritance
class Admin(User):
    ...


"""
* Assignment: OOP Override Super
* Type: class assignment
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min

English:
    1. Use class `User`
    2. Use class `Admin` inheriting from `User`
    3. In `Admin.login()` method call `login()` method of a parent class
    4. Use `super()` method
    5. Do not change inheritance
    6. Run doctests - all must succeed

Polish:
    1. Użyj klasy `User`
    2. Użyj klasy `Admin` dziedziczącej po `User`
    3. W metodzie `Admin.login()` wywołaj metodę `login()` klasy nadrzędnej
    4. Użyj metody `super()`
    5. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> mark = User('Mark', 'Watney')
    >>> mark.login()
    User login

    >>> melissa = User('Melissa', 'Lewis')
    >>> melissa.login()
    User login
"""


class User:
    def __init__(self, firstname, lastname):
        self.firstname = firstname
        self.lastname = lastname

    def login(self):
        print('User login')


# In `Admin.login()` method call `login()` method of a parent class
# Use `super()` method
# Do not change inheritance
class Admin(User):
    def login(self):
        ...