Exposing your Parent
By default, R6 classes only have access to the functionality of their direct parent. To allow access across multiple generations, the intermediate classes need to define an active binding that exposes their parent. This takes the form
active = list(
super_ = function() super
)
This exercise is part of the course
Object-Oriented Programming with S3 and R6 in R
Exercise instructions
A microwave oven class has been predefined for you in your workspace.
- Extend the fancy microwave oven class by adding an
active
list element. - Add an active binding to expose its parent's functionality.
- The binding should be called
super_
. - It should be a function that takes no arguments and simply returns
super
.
- The binding should be called
- Instantiate a fancy microwave object and assign it to
a_fancy_microwave
. - Call the fancy microwave's
super_
binding.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Expose the parent functionality
fancy_microwave_oven_factory <- R6Class(
"FancyMicrowaveOven",
inherit = microwave_oven_factory,
public = list(
cook_baked_potato = function() {
self$cook(3)
},
cook = function(time_seconds) {
super$cook(time_seconds)
message("Enjoy your dinner!")
}
),
# Add an active element with a super_ binding
___ = ___(
___ = ___
)
)
# Instantiate a fancy microwave
a_fancy_microwave <- ___
# Call the super_ binding
___