Untitled

mail@pastecode.io avatar
unknown
python
17 days ago
523 B
1
Indexable
Never
# 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!
Leave a Comment