Inheritance
To avoid redefining attributes and methods for similar classes, we can write a single base class from which more specialized classes inherit. For example, we can write a class called Pet
and define Dog
as a subclass of Pet
:
class Pet:
def __init__(self, name, owner):
self.is_alive = True # It's alive!!!
self.name = name
self.owner = owner
def eat(self, thing):
print(self.name + " ate a " + str(thing) + "!")
def talk(self):
print(self.name)
class Dog(Pet):
def talk(self):
super().talk()
print('This Dog says woof!')
Inheritance represents a hierarchical relationship between two or more classes where one class is a more specific version of the other: a dog is a pet (We use is a to describe this sort of relationship in OOP languages, and not to refer to the Python is
operator).
Since Dog
inherits from Pet
, the Dog
class will also inherit the Pet
class's methods, so we don't have to redefine __init__
or eat
. We do want each Dog
to talk
in a Dog
-specific way, so we can override the talk
method.
We can use super()
to refer to the superclass of self
, and access any superclass methods as if we were an instance of the superclass. For example, super().talk()
in the Dog
class will call the talk
method from the Pet
class, but passing the Dog
instance as the self
.