Python Programming - Inheritance - Discussion

Discussion Forum : Inheritance - General Questions (Q.No. 67)
67.
Consider the following code:
class Animal:
    def speak(self):
        print("Animal speaks")

class Dog(Animal):
    def speak(self):
        print("Dog barks")

def main():
    my_pet = Dog()
    my_pet.speak()
What will be the output of the main() function?
Animal speaks
Dog barks
The code will result in an error
Animal barks
Answer: Option
Explanation:
The speak() method is overridden in the Dog class, so calling speak() on a Dog instance will print "Dog barks".
Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.