Extends and Implements in Mootools

Today we are going to talk about Mootools and inheritance.

Sometimes we want to encapsulate some piece of code in a class to use it in several classes. There are two ways to use the code in a class in another class: extend the class or implement it.

The first way is use the property ‘Extends’. With this property, methods with the same name as the extend class will have a parent property used to call the super class method. You can override methods or add new behavior to them.

For example, we have the next class:

Now we want to use this class but methodA is incomplete for us. We are going to create a new class B and override methodA to add this new behavior:

Now we have a class that use the method in a super class and add new behavior to it.

One think you have to know is you only can extend one class.

Another way to add new behavior to a class is implementing other classes. For this we use the property ‘Implements’. This property is an array of other classes. All the properties that classes contain will be copied to the new class.

Following the previous example, we create a class that implements B and use methodB in its own methods:

You can use methodB but you can override it.

You can implement some classes instead only one if you use ‘Extends’.

This two properties can be combined if you need. In the following code we have a class E that extends A and implements D. It can override methods for A but not for D:


If you need extend a class you have to use the property ‘Extends’. This property is ALWAYS located at the beginning of the subclass declaration. It will be the first declaration.

If you need implement another classes, you have to use the property ‘Implements’. This property is ALWAYS located after the ‘Extends’ declaration, if it exists.

After theses properties we introduce the rest of code of the new class.