Untitled

 avatar
unknown
python
a year ago
523 B
4
Indexable
# Base class
class Animal:
    def __init__(self, name):
        self.name = name
    
    def speak(self):
        pass  # This method will be overridden by subclasses

# Subclass
class Dog(Animal):
    def speak(self):
        return f"{self.name} says Woof!"

# Another subclass
class Cat(Animal):
    def speak(self):
        return f"{self.name} says Meow!"

# Using the classes
dog = Dog("Buddy")
cat = Cat("Whiskers")

print(dog.speak())  # Output: Buddy says Woof!
print(cat.speak())  # Output: Whiskers says Meow!
Editor is loading...
Leave a Comment