Colmjude

/ notes
Notes
A collection of thoughts, things I've learnt and things I want to remember

Python class inheritance

An example of class inheritance in python.

class Animal:
    def __init__(self, name, type="Animal"):
        self.type = type
        self.name = name

    def print_info(self):
        print(f"My name is {self.name}, I'm an {self.type}")

class Dog(Animal):
    # child class can be instaniated with more args
    def __init__(self, name, type="Dog"):
        super().__init__(name, type)

    def say_hello(self):
        print("Hello")
        # call method from parent class
        self.print_info()