Object Oriented Programming

Inheritance in Python

Inheritance is used to inherit another class' members, including fields and methods. A sub class extends a super class' members.

class SubClass(BaseClass):
    def some_method(self, passed_params):
        #calling base class version of method
        BaseClass.some_method(self, passed_params)


Sub classes inherit methods and members from the base class. If a subclass has a method with the same name as the base class, the subclass' method overrides the base class method.

class Cat(object):
    def __init__(self, name):
        self.name = "Garfield"

    def sound(self):
        return "Meow"

class Tiger(Cat):
    def __init__(self, name): #will overwrite Cat constructor
        self.name = "Mike Tyson Jr."

    def sound(self): #will overwrite the Cat sound method
        return "Roar"

    def cat_sound():
        Cat.sound() #will call cat's sound

See Also

Classes - Python Documentation

Add to Slack

< Methods   |   Print >

© 2019 SyntaxDB. All Rights Reserved.