I'm having a little trouble understanding these two particular concepts in Python. I feel like I get the gist of them, but don't understand them completely.
From what I have gathered, super() calls the init method from the parent class and handles the arguments you pass to it from the parent class.
Why then would there need to be an init method in the subclass that includes the same parameters as those in the superclass? Is it because both classes are still similar in nature and the subclass would therefore require some of the basic features of the superclass? I know that most classes require a constructor, just wondering why you would need to re-define the same parameters in a subclass constructor like those in the superclass, and have a super() function within the body of that constructor.
An ex of code, would be something like this:
#9-6 Ice Cream Stand
class Restaurant:
def __init__(self, restaurant_name, cuisine_type):
"""Set attributes of a restaurant"""
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
self.number_served = 0
def describe_restaurant(self):
"""Print a statement about the restaurant"""
print(f"Welcome to {self.restaurant_name.title()}! Enjoy our amazing {self.cuisine_type.title()} cuisine!")
def open_restaurant(self):
"""Print a message indicating the restaurant is open"""
print(f"{self.restaurant_name.title()} is open and ready to serve!")
def set_number_served(self, total_served):
self.number_served = total_served
def increment_number_served(self, additional_served):
self.number_served += additional_served
class IceCreamStand(Restaurant):
def __init__(self, restaurant_name, cuisine_type='ice cream'):#Same params as in superclass contructor
"""Initialize the attributes of the parent class
Then initialize the attributes specific to an ice cream stand."""
super().__init__(restaurant_name, cuisine_type)
self.flavors = []
def display_flavors(self):
"""Display the ice cream flavors"""
for flavor in self.flavors:
print(f"- " + f"{flavor}")
ic_stand1 = IceCreamStand('tasty ice cream')
ic_stand1.flavors = ['chocolate', 'vanilla', 'strawberry']
ic_stand1.display_flavors()
ic_stand1.describe_restaurant()
Can anyone kindly walk this through for me, so I can get a better picture and grasp of these concepts? All help is appreciated.