Sometimes when using inheritance, you want the subclass to be able to perform a different action when the same method is called. Take our makeSound method from the previously created Animal class. It prints out “roar”, but that’s not the sound you want dogs making when you create your Dog class. Instead, we use the concept of method overriding to change what the method does. Within the subclass, we redefine the method (with the same name) to perform the task differently. Python will always use the method defined within the subclass first, and if one doesn’t exist, then it will check the superclass. Let’s use method overriding to alter the makeSound method and print the proper statement for our Dog class:
1| # overriding methods defined in the superclass 3| class Animal( ):
4| def makeSound(self):
5| print("roar") 7| class Dog(Animal):
8| def makeSound(self):
9| print("bark")
11| sam, lion = Dog( ), Animal( ) # declaring multiple variables on a single line
13| sam.makeSound( ) # overriding will call the makeSound method in Dog
15| lion.makeSound( ) # no overriding occurs as Animal does not inherit anything
Go ahead and run the cell. On the 8th line, we declare two instances sam and lion. The next line is where we call the makeSound method from our dog instance of sam. The output results in “bark” because of method overriding. As the method was inherited, but then redefined within the Dog class, it prints bark instead. On the 10th line, we call the same method with our Animal instance lion. This output is “roar” because lion is an instance of the Animal class. Remember that inheritance does not work backward.
Subclasses cannot give superclasses any features.
Inheriting Multiple Classes
Thus far, we’ve seen how we can inherit from a single superclass. Now we’re going to try inheriting from multiple classes. The main difference is how you super the attributes. Rather than using the super method, you call the class name directly and pass in the self parameter with the attributes. Let’s see how:
1| # how to inherit multiple classes 3| class Physics( ):
4| gravity = 9.8 6| class Automobile( ):
7| def __init__(self, make, model, year):
8| self.make, self.model, self.year = make, model, year # declaring all attributes on one line
10| class Ford(Physics, Automobile): # able to access Physics and Automobile attributes and methods 11| def __init__(self, model, year):
12| Automobile.__init__(self, "Ford", model, year) # super does
not work with multiple
14| truck = Ford("F-150", 2018)
16| print(truck.gravity, truck.make) # output both attributes
Go ahead and run the cell. We’ll get an output of 9.8 and “Ford”. On line 7 you’ll notice that we inherit two classes within the parenthesis for the Ford class. The 9th line is where the magic occurs this time though. Instead of using super, we initialize the variables by calling the name of the inherited class directly. Using the init method, we pass the self parameter along with all the attributes that Automobile requires. Python knows which superclass to use because of the name at the beginning of the line. On the last line, we’re able to see that we have access to both attributes declared within Physics and Automobile, where we are inheriting from.
Do'stlaringiz bilan baham: |