Building a Python OOP Console Application
Goal
Create a multi-file Python console application using inheritance and polymorphism.
Implementation
Base Class
## animal.py
class Animal:
def __init__(self, name, species):
self.name = name
self.species = species
def make_sound(self):
pass # Abstract method
Derived Classes
## dog.py
from animal import Animal
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name, species="Dog")
self.breed = breed
def make_sound(self):
return "Woof!"
## cat.py
from animal import Animal
class Cat(Animal):
def __init__(self, name, color):
super().__init__(name, species="Cat")
self.color = color
def make_sound(self):
return "Meow!"
Main Application
## app.py
from dog import Dog
from cat import Cat
def main():
dog1 = Dog("Buddy", "Labrador")
cat1 = Cat("Whiskers", "Orange")
print(f"{dog1.name} the {dog1.species} says: {dog1.make_sound()}")
print(f"{cat1.name} the {cat1.species} says: {cat1.make_sound()}")
if __name__ == "__main__":
main()
Run
python app.py
Output:
Buddy the Dog says: Woof!
Whiskers the Cat says: Meow!