Multilevel inheritance
You've gotten your hands dirty performing both single and multiple inheritance. In this exercise, you'll implement multilevel inheritance by building a new version of the Smartphone
class.
To help you get started, the Computer
and Tablet
classes have been defined and provided below. It' important to note that Tablet
inherits from the Computer
class. Good luck!
class Computer:
def __init__(self, brand):
self.brand = brand
def browse_internet(self):
print(f"Using {self.brand}'s default internet browser.")
class Tablet(Computer):
def __init__(self, brand, apps):
Computer.__init__(self, brand)
self.apps = apps
def uninstall_app(self, app):
if app in self.apps:
self.apps.remove(app)
This exercise is part of the course
Intermediate Object-Oriented Programming in Python
Exercise instructions
- Define a
Smartphone
class that inherits fromTablet
, call the parent constructor, and define thephone_number
instance-level attribute. - Create a
send_text
method that shares a textmessage
with arecipient
from theSmartphone
'sphone_number
. - Instantiate a
Smartphone
object calledpersonal_phone
and call its.browse_internet()
method; uninstall theWeather
app, and send the messageTime for a new mission!
to Chuck, via text.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create a Smartphone class that inherits from Tablet
class ____(____):
def __init__(self, brand, apps, phone_number):
____.____(self, ____, ____)
self.phone_number = ____
# Create send_text to send a message to a recipient
def ____(self, message, recipient):
print(f"Sending {____} to {____} from {____.____}")
# Create an instance of Smartphone, call methods in each class
____ = Smartphone("Macrosung", ["Weather", "Camera"], "801-932-7629")
personal_phone.____()
personal_phone.____("____")
personal_phone.____("____", "Chuck")