Encoding with Polymorphism

Create a class Encoder with a method encode(). Extend this class into Base64Encoder and URLEncoder, each implementing the encode() method appropriately.

Example 1:

Input: Base64Encoder 
Output: "Encoding in Base64"
Example 2:
Input: URLEncoder
Output: "Encoding URL"

Override the encode() method in each subclass to return the respective encoding method.

class Encoder:
    def encode(self):
        pass

class Base64Encoder(Encoder):
    def encode(self):
        return "Encoding in Base64"

class URLEncoder(Encoder):
    def encode(self):
        return "Encoding URL"

# Test the classes
base64encoder = Base64Encoder()
print(base64encoder.encode())  # Output: Encoding in Base64

urlencoder = URLEncoder()
print(urlencoder.encode())  # Output: Encoding URL

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!