Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions object oriented programing .py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Task #32: Inheritance and Method Overriding Example

class Animal:
def __init__(self, name):
self.name = name

def sound(self):
return "Some generic animal sound"

# Dog inherits from Animal
class Dog(Animal):
def sound(self):
return "Woof! Woof!"

# Cat inherits from Animal
class Cat(Animal):
def sound(self):
return "Meow!"

# Demonstration
animals = [Dog("Buddy"), Cat("Kitty"), Animal("Creature")]

for animal in animals:
print(f"{animal.name} says: {animal.sound()}")