Get startedGet started for free

Multiple Levels of Inheritance

1. Multiple Levels of Inheritance

R6 allows multiple levels of inheritance. Previously you saw how the fancy microwave inherited from the regular microwave. Our budget didn't stretch to another microwave,

2. microwave

so you'll have to make do with this image of a high-end microwave. The high-end microwave has all the features of the fancy microwave and more. Just imagine all the delicious food you could cook with it. In order to represent the high-end microwave using R6, you need

3. fancy microwave

a chain of inheritance. So the fancy microwave inherits from the regular microwave, and then

4. high end microwave

the high-end microwave inherits from the fancy microwave. Going back to

5. thing_factory

our Thing templates, the code is much as you might expect. As before, the child thing factory uses the inherit argument to inherit from the Thing class. Next, the grand child thing factory uses inherit to inherit from the ChildThing class. So far, so easy. The tricky part comes in understanding how you access methods at different levels of parenting. To see how it works,

6. thing_factory

let's give each class a do_something method. The parent do_something method prints a message explaining that it is the parent.

7. child_thing_factory

The child do_something method prints a message explaining that it is the child. And finally,

8. grand_child_thing_factory

the grandchild do_something method prints a message explaining that it is the grand-child. Focusing on the grandchild do_something method, you seen already that it can access its parent method using the super dollar prefix. This doesn't work for trying to access the grandparent method. For example, using super dollar twice won't work. Let's run the code to see what happens.

9. a_grand_child_thing

First, you create a grand-child object. Then when you call the do_something method, the grand-child message appears, then the child message appears, but then the parent method throws an error. Using super dollar twice isn't allowed. In fact, by default, R6 classes only have access to their direct parent. The grand-child class cannot access its grand-parent. There is a way around this. If you need to allow access across multiple generations,

10. child_thing_factory

the intermediate classes can make use of active bindings to expose their own parent. In this case, you need to modify the ChildThing class. The active binding you need to add is read-only, and it simply returns super. By convention, this binding is called super with an underscore, since "super" is a reserved word. Now only a small modification is need to the grand-child class' do_something method. To access its grand-parent method,

11. grand_child_thing_factory

the super dollar prefix is used to access the middle generation, then super underscore dollar is used to access the oldest generation.

12. a_grand_child_thing

Now, when you call this function, all three generations of the do_something method display their message.

13. Summary

To summarize, by default R6 objects only have access to functionality from their direct parent class. To access functionality across multiple generations, the intermediate generations must expose their parent using an active binding. This active binding is conventionally named super with an underscore, and simply returns the super object.

14. Let's practice!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.