Get startedGet started for free

Embrace, Extend, Override

1. Embrace, Extend, Override

Simply creating a new class that inherits from another class isn't useful by itself.

2. lego car

The fancy microwave class that you defined in the previous exercises has the same functionality as the parent microwave class. What you really want is for it to do something new. There are two ways to add this new functionality. You can you can override the existing functionality inherited from the parent, or extend the class to add brand new functionality. For example, here is a Lego car. I can override the behavior by swapping the existing cab, which appears to be driven by a dog for this big red cab. I can also extend the functionality by adding something new - in this case a tow hook. Let's take a look some code,

3. thing_factory

using a template Thing class, and its inherited ChildThing class. the parent Thing has a single method called do_something that simply prints a message explaining that it has been called.

4. child_thing_factory

The ChildThing starts by inheriting from the Thing class. To override functionality, you define elements with the same name as those in the parent. For example, here the child class has its own do_something method. To extend the functionality, you simply define new public methods, or private data fields. For example, adding a do_something_else method adds functionality to the child class. This function is only available in the child; it is not available in the original parent class. Let's create

5. a_child_thing

a child object and see what happens when you call the do_something method. As you can see, it is the child do_something method that is called. However, the child class also has access to the functionality from the parent that has been overridden. To make use of this feature, you need to know about two keywords, "self", and "super". Previously, you saw that public methods can access private data fields by using a private dollar prefix.

6. accesses

In the same way, they can access other public methods by using a self dollar prefix. Similarly, they can access public methods in their parent class by using a super dollar prefix. In object-oriented programming terminology, the parent class is sometimes called the super class. For a concrete example,

7. child_thing_factory

let's make some changes to the do_something_else method. Now this function prints a message stating its name. Then it calls the do_something method in itself, using the self dollar prefix. Finally it calls the do_something method in its parent, using the super dollar prefix.

8. a_child_thing

Let's re-create the child thing object, and call the do_something_else method. The printed output makes it clear which methods are called.

9. Summary

To summarize, inherited classes can define new functionality in two ways. They can override functionality by defining methods with the same names as those in the parent class. Or they can extend the parent class by defining methods with completely new names. In the same way that private fields can be accessed using the private dollar prefix, public methods can be accessed using the self dollar prefix. Similarly, public methods in the parent object can be accessed using the super dollar prefix.

10. Let's practice!