16.10. Inheritance¶
Another powerful feature of object-oriented programming is the ability to create a new class by extending an existing class. When extending a class, we call the original class the parent class and the new class the child class.
For this example, we move our PartyAnimal
class into its own file.
Then, we can ‘import’ the PartyAnimal
class in a new file and extend it, as follows:
class PartyAnimal:
def __init__(self, nam):
self.name = nam
print(self.name,'constructed')
def party(self, x) :
self.x = x
self.x = self.x + 1
print(self.name,'party count',self.x)
When we define the CricketFan
class, we indicate that we are extending
the PartyAnimal
class. This means that all of the variables (x
) and methods
(party
) from the PartyAnimal
class are inherited by the CricketFan
class.
For example, within the six
method in the CricketFan
class, we
call the party
method from the PartyAnimal
class.
As the program executes, we create s
and j
as independent instances
of PartyAnimal
and CricketFan
. The j
object has additional capabilities beyond
the s
object.
Sally constructed
Sally party count 1
Jim constructed
Jim party count 1
Jim party count 2
Jim points 6
['__class__', '__delattr__', ... '__weakref__',
'name', 'party', 'points', 'six', 'x']
In the dir
output for the j
object (instance of the CricketFan
class), we see
that it has the attributes and methods of the parent class, as well as the attributes
and methods that were added when the class was extended to create the CricketFan
class.