I am trying to implement an interface and this interface is taken by two concrete classes say class First and class Second, I have another class that takes these two classes as a parent class CO. The class CO makes a decision based on a flag to return which of the two inherited classes, so as to use their implementation.
from abc import ABC, abstractmethod
class common(ABC):
@abstractmethod
def firstfunction(self):
pass
@abstractmethod
def secondfunction(self):
pass
class First(common):
def __init__(self)-> boto3:
self.ert = "danish"
# self.username = kwargs["username"]
# self.password = kwargs["password"]
print("Inside First function")
def firstfunction(self):
print("My implementation of the first function in FIRST CLASS")
def secondfunction(self):
print("My implementation of the second function in FIRST CLASS")
class Second(common):
def __init__(self):
self.rty = "pop"
# self.session_id = kwargs["session_id"]
print("Inside Second function")
def firstfunction(self):
print("My implementation of the first function in SECOND CLASS")
def secondfunction(self):
print("My implementation of the second function in SECOND CLASS")
class CO(First, Second):
def __init__(self):
self.inst = self.jo()
def jo(self):
a = True
if a:
main_object = First()
return main_object
else:
main_object = Second()
I am instantiating and calling the methods
mymainclass = co()
objt = mymainclass
objt.firstfunction()
objt.secondfunction()
So my condition is if the flag a = True in the CO class then the Concrete class First implementation of the methods should be used and we should get the output like this:
Inside the First function
My implementation of the first function in FIRST CLASS
My implementation of the second function in FIRST CLASS
If the flag a = False in the CO class then concrete class Second should be used and we should get the output like this:
Inside the Second function
My implementation of the first function in FIRST CLASS
My implementation of the second function in FIRST CLASS
From the given code I am getting the following output for the flag a = False :
Inside the Second function
My implementation of the first function in FIRST CLASS
My implementation of the second function in FIRST CLASS
Can someone make this code work? What am I doing wrong? I know I can make a function that takes these two classes as variables and then return what I want based on a condition. But I want to use classes for the same