Why and how of using abstract classes in Typescript.
We have seen a bit of classes and objects in Typescript previously. There was however a key aspect of object oriented programming missing there. Although we discussed inheriting classes, we never mentioned abstract classes.
Sacrilege, that.
On the other hand, anything goes for the daily blogging gobble-gobble.
Abstract classes are meant to be inherited and are not used directly. Defining such an abstract class is painfully easy.
|
|
Using abstract classes is easy too..
|
|
Sorry, wrong code block there. Let me correct that.
|
|
Let’s go ahead and throw an abstract method there and see the fun unravel.
|
|
If you are wondering whatever the hell happened there -
- Create abstract class
- Introduce a silly abstract method to
console.log
something - Extend
Planet
with a class aptly calledEarth
.Earth
must implement the abstract method, else compilation fails - Create an instance of the derived class, again aptly named as
earth
- Make everyone reach nirvana by
consoling
some statements