Extending the Cooking Capabilities
The child class can extend the functionality of the parent by adding further public methods with names that are different to those available in the parent class.
Public methods can call other public methods by prefixing their name with self$
.
This exercise is part of the course
Object-Oriented Programming with S3 and R6 in R
Exercise instructions
A microwave oven has been predefined in your workspace.
- Extend the definition of the fancy microwave oven class to include a
public
element. - Add a public
cook_baked_potato()
method.- This should take no arguments.
- In the body, it should call its own
cook()
method for 3 seconds.
- Create a
FancyMicrowaveOven
object and assign it toa_fancy_microwave
. - Call
a_fancy_microwave
'scook_baked_potato()
method.
Warning: Do not try to eat baked potatoes after they have been cooked for 3 seconds as they will taste awful and you may suffer food poisoning.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Explore microwave oven class
microwave_oven_factory
# Extend the class definition
fancy_microwave_oven_factory <- R6Class(
"FancyMicrowaveOven",
inherit = microwave_oven_factory,
# Add a public list with a cook baked potato method
___ = ___(
___ = ___
)
)
# Instantiate a fancy microwave
a_fancy_microwave <- ___
# Call the cook_baked_potato() method
___