Understanding Concept of TypeScript Day 11


Abstract Classes :
  • Abstract class is a half defined parent class.

  • Abstract classes are base classes from which other classes can extend.They cannot be instantiated themselves.

  • The two key characteristics of an abstract class in TypeScript are:

    1) They can implement methods of their own.

    2) They can define methods that inheriting classes must implement.

  • For this reason, abstract classes can conceptually be considered a combination of an interface and a class.

Interfaces :
  • Interface is a CONTRACT, LEGAL <, ENFORCEMENT - impact analysis.

  • An interfaces specifies a list of fields and functions that may be expected on any class implementing the interface.

  • Conversely, a class cannot implement an interface unless it has every field and function specified on the interface.


Need Of Interfaces :
  • The primary benefit of using interfaces, is that it allows one to use objects of different types in a polymorphic way.

  • This is because any class implementing the interface has at least those fields and functions.

  • Declare public variables and methods type in the interface to define how other typescript code can interact with the interface.


Difference Between Abstract Class & Interface :
  • Differences between abstract class and interface that are given below :

Abstract Class :
  • Some members are abstract and some are fully implemented.

  • Abstract class does not support multiple inheritances.

  • Abstract class compile to JavaScript functions.

  • Abstract classes are related. For example ViewModelBase is an abstract, class then we know this class will only inherits by ViewModels.

Interface :
  • All members are abstract.

  • Interfaces support multiple inheritances.

  • TypeScript Interface has zero JavaScript code that means it is only available in TypeScript and does not produce any code in compiled JavaScript file.

  • Interfaces are generic in nature. They can be implemented by any class for example IClone interface can be implemented by any class like business objects, database classes.

Example :

Program-

             

Comments

Popular posts from this blog

Basic Concept In C Sharp Day 16

Understanding C Sharp Day 18

Concept In Angular Day 14