Understanding operator overloading
Some programming languages, such as C# and Python, allow you to redefine specific operators to work in a different way based on the classes in which we apply them. For example, we can use comparison operators—such as less than (<
) and greater than (>
)—to return the results of comparing the age value when they are applied to instances of Dog
.
Tip
The redefinition of operators to work in a specific way when applied to instances of specific classes is known as operator overloading.
An operator that works in one way when applied to an instance of a class may work differently on instances of another class. We can also override the overloaded operators in subclasses. For example, we can make comparison operators work ...
Get JavaScript : Object-Oriented Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.