Inheritance - Parent & Child Classes
Inheritance - Parent & Child Classes
Inheritance
Defining Inheritance
Imagine you want to create two Python classes, Person and Superhero.
These respective classes might look something like this:
No_Inheritance
There are some similarities between the Person class and the Superhero
class. If the Person class already exists, it would be helpful to “borrow”
from the Person class so you only have to create the new attributes and
methods for theSuperhero class. This situation describes inheritance — one
class copies the attributes and methods from another class.
Inheritance Syntax
In the IDE on the left, the Person class is already defined. To create the
Superhero class that inherits from the Person class, add the following code
at the end of the program. Notice how Superhero takes Person as a
parameter. This is how you indicate to Python that the Superhero class
inherits from the Person class. You can also say that the Person class is the
parent and the Superhero class is the child.
class Superhero(Person):
pass
The Superhero class can access the attributes of the Person class because
Superheor inherits from Person.
challenge
print(hero.age)
hero.say_hello()
Visualizing Inheritance
Python has a function called help which can aid you in understanding
what is being inherited. Add the following line of code after instantiating
hero as an instance of the Superhero class.
You should see a visual representation of the Superhero class. The first
section is Method resolution order. This shows the steps Python takes with
inheritance. Python first starts with the Superhero class. So when you say
print(hero.name), Python will look to the Superhero class for the attribute
name. This does not exist. So Python moves to the next step, which is the
Person class. Since the Person class has a name attribute, Python can stop
here. Notice too that the say_name and say_age methods are also inherited.
challenge
class Superhero(Person):
def say_hello(self):
super().say_hello()
The Superhero class has a method called say_hello. The super() keyword
tells Python to go to the parent class; the .say_hello() tells Python to call
this method. So super().say_hello() is calling say_hello from the Person
class.
challenge
class Superhero(Person):
def say_hello(self):
super().say_hello()
def say_age(self):
super().say_age()
If the super() keyword is used to call methods from the parent class, how
come super() was not used with the __init__ method? In fact, the __init__
was never called in the Superhero class. A child class will automatically
inherit the __init__ method if it is not defined. __init__ is called when an
object is instantiated, and super() does not need to be used.
challenge
class Superhero(Person):
def say_two_things(self):
super().say_hello()
super().say_age()
Inheritance Hierarchy
You have seen how the Superhero class becomes the child of the Person class
though inheritance. The relationship between these two classes is called
inheritance (or class) hierarchy. Python has some built-in functions to help
you determine the hierarchy of classes.
The first function is isinstance. This function takes an object and a class
name. It returns True if the object is an instance of the class. Look at the
code on the left. ClassA is the parent of ClassB, and ClassC is the parent of
ClassD. The isinstance function can be used to test these relationships. Add
the following code to the program.
print(isinstance(object_b, ClassA))
print(isinstance(object_d, ClassC))
challenge
Is a Subclass?
Another function that can be used to determine inheritance hierarchy is
issubclass. This function takes a class and a class name. It returns true if
the class is a subclass (or child) of the class name. This is very similar to
isinstance, but the difference is important. issubclass checks to see if a
class (not an object) is the child of another class. Add the following code to
the program.
print(issubclass(ClassB, ClassA))
print(issubclass(ClassD, ClassC))
challenge