Modes of method inheritance - delegate - provide
Let the child implement the functionality.
examples/oop/inheritance/delegate.py
class Parent(): def greet(self): print("Hello", self.get_name()) class Child(Parent): def __init__(self, name): self.name = name def get_name(self): return self.name # Should not create instance from Parent p = Parent() # p.greet() # AttributeError: 'Parent' object has no attribute 'get_name' c = Child('Foo') c.greet() # Hello Foo
- Should we have a version of greet() in the Parent that throws an exception?
- Do we want to allow the creation of instance of the Parent class?
- Abstract Base Class (abc)