TypeScript — Object Oriented Programming
Object-oriented programming is programming paradigm based on the concept of objects, which can contain data, in the form of fields, and code, in the form of procedures.
Classes :
Class is user defined blueprint, it represents set of properties or methods that are common to all objects of one type.
Inheritance :
It is mechanism in which one object acquires all the properties and behaviours of parent object. You can reuse methods and fields of parent class.
Abstract Class :
It is class which that is declared abstract. Abstract classes are base classes from which other classes may be derived. It contains abstract and non-abstract methods.We can not initiate directly to the abstract classes.
Interface:
In typescript interfaces represents object type or structure, below we can define wheel structure
Interface — class type:
Like in other languages java, c# etc, In typescript also class can implement interface. Interface contains only abstract methods these methods does not have body.
Public, Private and Protected Modifiers :
- Typescript default variables are public, we can access variables outside the class.
- If we declare variable private, we can not access variable outside the class.
- In inheritance if we declare parent class variables as private, we cant access variables in subclass. To access variables in subclass we need to make it as protected in parent class.
Static variables & methods :
- Static variables are defined using static keyword, To access static variable no need to create object for class. we can directly access like this <classname>.<variable name>.The static variable gets memory only once in the class area at the time of class loading.
- Any function have prefix static that function treated as static function. Inside static function we can access only static variables. we can directly call static methods like this. <classname>.<method name>
Singleton :
It is creational design pattern. Its main aim is exists only one instance to the class through out life of application. To create singleton object we make it constructor as private, so that we cant create new instance to the class.
readonly, optional parameters :
- If you put prefix readonly to variable we can access the value inside and outside of the class, but we cant change the value. we can assign value to variable directly at the time of declare variable or inside class constructor.
- If you want to make parameter is optional just put (?) after variable name.
constructor , super :
- Constructor is special method which is used to initialize objects. It is called when the class object is created.
- Super is used to call parent class constructor method and we can send parameters data from subclass to parent class constructor using super.
Thanks for reading