abstract vs interface

ref –

  • https://stackoverflow.com/questions/479142/when-to-use-an-interface-instead-of-an-abstract-class-and-vice-versa
  • http://chineseruleof8.com/code/index.php/2020/12/24/abstract-typescript/
  • http://chineseruleof8.com/code/index.php/2021/03/24/interfaces-in-typescript/

An abstract class can have shared state or functionality
An interface is only a promise to provide the state or functionality

An abstract class allows you to create functionality that subclasses can implement or override

We can define instance variables and accept them as input using the abstract class constructor.

Remember that an abstract class is an abstraction, we can’t instantiate it directly. Trying anyway for demonstration purposes, we’ll notice that we get an error that looks like this:

We’ll introduce one of our specific types of Book concretions — the PDF class. We extend/inherit the abstract Book class as a new subclass to hook it up.
We can only extend one abstraction.

Even though we cannot instantiate an abstract class, we can treat it as a superclass and instantiate it by
1) extending from it
2) then call super on it in the constructor

An interface only allows you to define functionality, not implement it

And whereas a class can extend only one abstract class, it can take advantage of multiple interfaces

Abstract classes should be used primarily for objects that are closely related.

interfaces are best suited for providing common functionality to unrelated classes

If you are designing small, concise bits of functionality, use interfaces.

If you are designing large functional units, use an abstract class.

If you anticipate creating multiple versions of your component, create an abstract class. Abstract classes provide a simple and easy way to version your components. By updating the base class, all inheriting classes are automatically updated with the change.

Interfaces, on the other hand, cannot be changed once created in that way. If a new version of an interface is required, you must create a whole new interface.