Logo

dev-resources.site

for different kinds of informations.

Overloading vs Overriding in Typescript

Published at
5/13/2022
Categories
inheritance
overload
override
typescript
Author
smac89
Author
6 person written this
smac89
open
Overloading vs Overriding in Typescript

The main difference between overloading and overriding is this: The former is concerned with the signature of a function, whereas the later is concerned with the behaviour of a method.

Override

Override is something you do to change the behaviour of a method in a subclass. When you override a method, it is expected that you intend to change the default behaviour of the method, that was inherited from the parent class. However, the method signature should remain the same in order not to violate LSP.

Overload

Overload is something you do to functions; Not to change their behaviour*, but rather to diversify their signature. A function overload keeps the same name as the function, but it's free to introduce a different signature. You've probably seen an example of this if you come from a JavaScript background and seen functions which have parameters that can be one of two or more types:

/**
* @param {string|string[]} The object to use
*/
function stringOrArray(strOrArr) {
  ...
}
Enter fullscreen mode Exit fullscreen mode

It's difficult to tell that this is a function overload because JavaScript does not have a strong enough type system to disambiguate the overloads. However, with the Jsdoc comment, we can sort of make out that this function has an overload.
With typescript we can make this overload an actual part of our function signature, instead of living in a comment.

ex.

function stringOrArray(str: string)
function stringOrArray(arr: string[])
function stringOrArray(strOrArr: string | string[]) {
  ...
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

The implementation of the overloaded function stays the same, but the signature is allowed to be different. In the case of overriding, the implementation can be different, but the signature has to be the same, in order not to violate the Liskov Substitution Principle.

inheritance Article's
30 articles in total
Favicon
Code Smell 286 - Overlapping Methods
Favicon
Understanding Traits in PHP and How They Differ from Inheritance
Favicon
Understanding Classes and Inheritance in JavaScript
Favicon
Mastering Generalization in OOP: Techniques and Examples
Favicon
Upcasting — Using a Superclass Reference for a Subclass Object
Favicon
Mapping inheritance hierarchies with MapStruct
Favicon
Python Inheritance Explained: Types, Examples, and Best Practices
Favicon
Mastering TypeScript: Understanding the Power of extends
Favicon
PHP: Herança vs. Composição
Favicon
Method Resolution Order in Python 3
Favicon
Why we don't use RemoteWebDriver driver = new ChromeDriver()
Favicon
Understanding JavaScript Inheritance: A Deep Dive into Prototypal and Constructor Patterns
Favicon
Understanding Inheritance and Polymorphism: Simplified OOP Concepts
Favicon
JS Inheritance - Part 2: Factory Functions vs. Classes
Favicon
Inheritance with access-specifier in cpp
Favicon
Inheritance in Dart
Favicon
RoR - extend, < (inheritance), include - know the difference
Favicon
Inheritance vs composition: a fight against Egyptian gods
Favicon
Object Oriented Concept - Inheritance
Favicon
Exploring Component Inheritance in Aventus
Favicon
Python Inheritance
Favicon
Inheritance vs Composition: Using a Role-Playing Game in JavaScript as an Example
Favicon
PostgreSQL - Partitioning & Inheritance
Favicon
Prototype and Prototypical Inheritance
Favicon
Ruby - Inheritance
Favicon
MultiLevel Inheritance in Java
Favicon
Overloading vs Overriding in Typescript
Favicon
Java Error - at least one public class is required in main file
Favicon
Polymorphism in Java.
Favicon
Method Overriding in Java.

Featured ones: