I am trying to create a class in R6 which inherits a function from its parent class. However, that function relies on other 'helper' functions which are overwritten in the child class.
When I call the parent function, I want it to use the helper functions from the parent class, not the overridden ones from the child class.
Here is a mock-up of how my code works:
ParentClass <- R6Class(
public = list(
helper_fn = function() { print("Parent helper.") },
main_fn = function() { self$helper_fn() }
}
)
)
ChildClass <- R6Class(
inherit = ParentClass,
public = list(
helper_fn = function() { print("Child helper.") },
main_fn = function() { super$main_fn() }
)
)
The intended behaviour is to print "Parent helper." but when the child_class
calls the parent's main_fn
, it uses the overridden function from the child_class
.
child_class <- ChildClass$new()
child_class$main_fn()
# prints "Parent helper."
Is it possible to avoid this? Or is this just how overriding a function works?