Write a python program to implement Method Overriding.

 Write a python program to implement Method Overriding.



class Animal:

    """A parent class representing an animal."""

    def speak(self):

        """A generic method for animal sounds."""

        return "The animal makes a generic sound."


class Dog(Animal):

    """A child class representing a dog, inheriting from Animal."""

    def speak(self):

        """Overrides the speak method to provide a specific sound."""

        return "Bark!"


class Cat(Animal):

    """A child class representing a cat, inheriting from Animal."""

    def speak(self):

        """Overrides the speak method to provide a specific sound."""

        return "Meow"


# Create instances of the classes

generic_animal = Animal()

dog_instance = Dog()

cat_instance = Cat()


# Call the speak() method on each instance

print(f"Generic Animal says: {generic_animal.speak()}")

print(f"Dog says: {dog_instance.speak()}")

print(f"Cat says: {cat_instance.speak()}")





Comments