JS: Dive into Classes
Theory: Overriding Methods
Eliminating code duplication is not the only thing to use class inheritance. Sometimes, we can use it to change the existing behavior of a base class.
The <select> tag in the DOM is represented by the HTMLSelectElement class. It has additional methods needed to work with the list of elements. One such method is item(index). You can use it to retrieve a specific option from the list:
Let's imagine that we frequently need to access the elements of this list from the end. You would have to execute this kind of code all the time:
There's nothing criminal about this code, but it could be better. One possible solution to this problem is to extend the method's behavior and teach it to work with negative numbers. The ability to refer to indexes in reverse order is a common practice in many languages:
How do you do that? Inheritance makes it possible to override superclass methods. Take a look at this example:
The HTMLCustomSelectElement subclass created above overrides the item(index) method. Overriding means a subclass creates a method with the same name as the parent class.
Our new method does the extra work of calculating the index but still needs the original item(index) method to select the desired item.
To do so, we use a special syntax that specifies explicitly that we shpuld take the method from the parent class: super.item(realIndex).
Why did we need to use special syntax? Imagine if it had this code instead:
Which item() method should we take in this case — the one we're in right now or the parent one? Inheritance always chooses the method closest to the inheritance chain. So, a call through this will generate recursion, but the parent method will never be called.
For the same reason, it's not possible to call methods that are overridden in the descendants outside the object:
Overriding is not limited to one level of inheritance. We can override any overridden method again in the descendants of the current class.
Calling the parent class constructor works a little differently. To do this, use super as a function:
Using descendants
Creating a descendant class and starting to use it are two differences.
In situations where we create these classes, everything is simple. We replace the calls of the old class with the new one.
But if someone else created the objects of this class by their code, the task becomes more complicated. To substitute such a class, you need support for polymorphic behavior from the other person's code.
For example, when working with the DOM, objects of these classes are sometimes generated by the programmer or the system. For example:
Can we replace the class in the querySelector() example? It depends on the implementation of the DOM library. It isn't possible in the libraries that we know of. It means that the only way to use your class is to convert the returned object into a class object we want. Is it worth it? Almost certainly not:
In other words, inheritance to redefine behavior, while seemingly a logical step, has some usage limitations.

