Fruit Tastes with Polymorphism

Write a class Fruit with a method taste(). Implement child classes Apple, Banana, and Grape that override the taste() method to print their respective tastes.

Example 1:

Input: Apple 
Output: "Apple tastes sweet"

Example 2:

Input: Banana
Output: "Banana tastes sweet"

Example 3:

Input: Grape
Output: "Grape can taste sweet or sour"

Override the taste() method in each subclass to return the respective taste of the fruit.

class Fruit:
    def taste(self):
        pass

class Apple(Fruit):
    def taste(self):
        return "Apple tastes sweet"

class Banana(Fruit):
    def taste(self):
        return "Banana tastes sweet"

class Grape(Fruit):
    def taste(self):
        return "Grape can taste sweet or sour"

# Test the classes
apple = Apple()
print(apple.taste())  # Output: Apple tastes sweet

banana = Banana()
print(banana.taste())  # Output: Banana tastes sweet

grape = Grape()
print(grape.taste())  # Output: Grape can taste sweet or sour

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!