I ask this query because I'm looking at prototyping in JavaScript and I don't quite understand what they call "Overriding Methods" and I couldn't find a page in Spanish that explains it clearly.
So, in concrete and easy to understand words, what is "Overriding Methods"?
From already thank you very much.
Overriding methods stands for method overriding and is an object-oriented programming technique. This technique consists of overriding methods that have been inherited to add extra code or simply change/extend their functionality.
Imagine we have the function
Hero
:hero
As you can see, the function
Hero
has one propertyname
and two methods:getPowers
andhello
. The methodgetPowers
returns the list of the hero's powers andtoString
returns a customtoString
(inherited fromObject
) of the object, displaying its name and its powers.Now, we have a function called
Superman
:Superman
The first thing we notice when looking at the function
Superman
is that it inherits fromHero
. We can know this by the line:Below we see that we have redefined the method
getPowers
. This method is said to be overridden , since it overrides the original method ofHero
. In this overriding method we have extended its functionality to be able to add Superman's own powers.If we execute the previous code, we will see that it lists all the powers of
Superman
and not only the one ofHero
.full example