Consider the following code:class DailyRoutine: def morning(self): print("coffee") print("shower")class MondayRoutine(DailyRoutine): def morning(self): print("jog") print("coffee") print("shower")We want the morning method in the MondayRoutine class to add to the parent class's functionality, rather than completely overwrite it. How do we do this in a way that doesn't duplicate so much code?Group of answer choicesI don't know.Use super() in the MondayRoutine class.Use super().morning() in MondayRoutinemorning() method.
Question
Consider the following code:class DailyRoutine: def morning(self): print("coffee") print("shower")class MondayRoutine(DailyRoutine): def morning(self): print("jog") print("coffee") print("shower")We want the morning method in the MondayRoutine class to add to the parent class's functionality, rather than completely overwrite it. How do we do this in a way that doesn't duplicate so much code?Group of answer choicesI don't know.Use super() in the MondayRoutine class.Use super().morning() in MondayRoutinemorning() method.
Solution
We can achieve this by using the super() function in the MondayRoutine class. The super() function is used to call a method from a parent class into a child class. Here's how you can modify the MondayRoutine class:
class MondayRoutine(DailyRoutine):
def morning(self):
print("jog")
super().morning()
In this code, when the morning method of the MondayRoutine class is called, it first prints "jog", then it uses super().morning() to call the morning method from the parent DailyRoutine class, which prints "coffee" and "shower". This way, we're adding to the parent class's functionality without duplicating code.
Similar Questions
What does method overriding allow in Python? A method to have the same name but different parametersA subclass to provide a specific implementation of a method that is already defined in its superclassA class to have multiple methods with the same nameA class to use methods from another class
How does Python support method overriding?By renaming the method in the subclass.By defining a method in the subclass with a different signature than the one in the parent class.By defining a method in the subclass with the same name and signature as the one in the parent class.By using a special decorator to indicate the override
class Parent: def __init__(self): self.__num=56 def show(self): print("Parent:",self.__num)class Child(Parent): def __init__(self): super().__init__() self.__var=89 def show(self): print("Child:",self.__var)parent=Parent()parent.show()child=Child()child.show()
What is the purpose of the super() function in Python?ACall a method from the parent classBExecute a method multiple timesCCheck if a method is present in the classDInitialize a method in the child class
If a method in a super class is overridden by the sub class, then the overridden method can be invoked using ____________ keyword.Select one:a.thisb.superc.classd.extends
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.