Product Class Hierarchy

Implement a class Product and then define child classes for different categories like Electronics, Furniture, Groceries, each with a unique method description that returns the description of the respective product categories.

Example 1:

Input: Electronics 

Output: "Electronics: Devices that operate on electrical power, including computers, televisions, and smartphones."

Example 2:

Input: Furniture 

Output: "Furniture: Items used to make a space suitable for living or working, including tables, chairs, and beds."

Implement a method description in each of the child classes that returns the respective descriptions of the product categories.

class Product:
    def description(self):
        pass

class Electronics(Product):
    def description(self):
        return "Electronics: Devices that operate on electrical power, including computers, televisions, and smartphones."

class Furniture(Product):
    def description(self):
        return "Furniture: Items used to make a space suitable for living or working, including tables, chairs, and beds."

class Groceries(Product):
    def description(self):
        return "Groceries: Items of food sold in a supermarket."

# Test the classes
electronics = Electronics()
print(electronics.description())

furniture = Furniture()
print(furniture.description())

groceries = Groceries()
print(groceries.description())

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!