Plant Growth with Polymorphisma

Write a class Plant with a method grow(). Implement child classes Tree and Flower that override the grow() method to print their respective growth patterns.
Example 1:

Input: Tree
Output: "The tree is growing tall"

Example 2:

Input: Flower
Output: "The flower is blooming"

Override the grow() method in each subclass to return the respective growth pattern.

class Plant:
    def grow(self):
        pass

class Tree(Plant):
    def grow(self):
        return "The tree is growing tall"

class Flower(Plant):
    def grow(self):
        return "The flower is blooming"

# Test the classes
tree = Tree()
print(tree.grow())  # Output: The tree is growing tall

flower = Flower()
print(flower.grow())  # Output: The flower is blooming

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!