I would like to call a function in super class whe...
# getting-started
l
I would like to call a function in super class whenever my parent’s init gets executed. How do I do this automatically so I don’t have to remember to do this in every child class that I create.
j
call the function in the parents init? Not sure I understand the question
l
I am building a framework and don’t want to force every parent class not to forget to call something in the super class. Was hoping I can make it easier to prevent framework users from making this mistake by calling the super function automatically for them
j
Maybe post a sample hierarchy that DOES force the responsibility on the children?
l
Ok so basically my main reason to do it was to watch for a StateFlow to be initiated which happens by the parent ViewModel class. So what I ended up doing is using the init of the parent abstract ViewModel to watch for that value to be non-null by using a coroutine job that polls every 1ms that works
k
James, an actual code sample might help to clarify. Personally I’m struggling to understand because most often the word super means the same thing as parent, but you appear to be using them to mean different things. Generally I would say: • Base class/Super class/Parent class all mean the same thing • Then the class which extends the class above would be the child class or subclass So, if you want to make sure something always gets called in the init, then you would put it in the parent class, yes? And then the child classes don’t have to call it. But again, if I’m misunderstanding, please post some code to clarify.