Using Inheritance in Your Classes
00:00
Back where we left off in VS Code, you have your Starship class. Below that, define a class ScienceVessel that inherits from Starship with pass as the class body. Save the file, and open up the REPL.
00:15 Start by importing the two classes,
00:22
and now create a ScienceVessel. discovery = ScienceVessel( passing in "Discovery", 5)
00:32
See what happens when you print(discovery).
00:36
Exploration Vessel - Discovery. The __str__() method from the parent is doing its job, and you can use its methods too. Try discovery.set_destination("the Monolith"),
00:49
and 'Discovery sets a course for the Monolith'. This is also a chance to see polymorphism in action. Use the built-in isinstance() function to confirm that discovery is a member of the class ScienceVessel.
01:02
isinstance passing in (discovery, ScienceVessel) returns True. And if you try with Starship, isinstance( passing in discovery, Starship) it also returns True, proof that subclasses are considered instances of their parent classes.
01:21
And now it is time to finish writing the ScienceVessel class. I’ll exit the REPL, remove the pass statement, add a class attribute ship_type set to "Science Vessel"
01:35
and an __init__() method: def __init__ taking in (self, name, crew_size and a new parameter labs): to track how many labs are on the Science Vessel.
01:45
To avoid repeated code, you can invoke the __init__() of Starship with super(), passing in (name, crew_size)
01:55
and set self.labs to labs. Next, you can also extend the __str__() method. def __str__(self): base_str = the result of calling super().__str__().
02:10
And to this base string, you’ll add a line that describes the number of research labs on board. Return the f-string f"{base_str} | Research labs: {self.labs}".
02:26
And hey, why not add a new method? It’s a science vessel, so let’s go with survey(). def survey with the required (self): parameter again returns the f-string f"{self.name} surveys the sector."
02:40
Now save the file and open up the REPL again to give this new ship a spin: python. Import both classes: from starship import ScienceVessel, Starship. Create a Starship: prometheus = Starship passing in ("Prometheus", 117)
03:02
and create a ScienceVessel. How about Voyager? Remember, Science Vessels take a third argument, research labs: voyager = ScienceVessel( "Voyager", 151 for crew size and 6 for number of research labs.
03:19
Go ahead and print(prometheus): Exploration Vessel - Prometheus. And print(voyager). Look at that: Science Vessel - Voyager | Research labs: 6.
03:33
The updated __str__() works. You can try ScienceVessel’s new survey() method: voyager.survey(), and 'Voyager surveys the sector'. Of course, if you try that with a regular starship like prometheus, prometheus.survey() returns an AttributeError because Starship objects do not have a survey attribute.
03:55
And of course, voyager has its inherited methods too. Try voyager.set_destination passing in ("the Alpha Quadrant"), and 'Voyager sets a course for the Alpha Quadrant'.
04:07 That is going to take about 70 years, and that’s inheritance. Believe me, you’ve only just scratched the surface. In addition to what you’ve done so far, you can also have multiple classes that all inherit from the same base class.
04:20
And you can use subclasses like ScienceVessel as parent classes for further derived classes, which in turn inherit from all of their ancestors.
04:29 A word of warning though: class hierarchies can quickly become complicated, so I advise you to start small and simple whenever you can. And please stick around for the next and final lesson, where we’ll wrap things up in the summary.
Become a Member to join the conversation.
