Access Modifiers in TypeScript

Typescript support four types of access modifiers: public, private, protected and read-only. Access modifiers are used to encapsulate class and its member variables.

Let's explore the definitions of the four types of access modifiers:

Public: Public member variables are accessible anywhere in a class or a program. By default, Typescript member variables are public so declaring public member variables using keyword public is optional. For example, in an astronomical calculation the speed of light value 186000 (miles per second) can be set as public.

Protected: Protected member variables defined in a class can be accessible only within its class or its derived class. For example, a joint bank account number can be defined as protected.

Private: Private member variables scope is limited only within its class. Private member variables are not accessible in derived class. A personal diary of a person can be defined as a private member variable.

Read-Only: Read-Only member variables do not allow changes to the value of variable once they are initialized.

The program below illustrates how access modifiers are implemented in Typescript. There is a Pipeline Terminal in the city, which consists of equipment like a valve. PipelineTerminal is a base or parent class. Valve is a derived class from the PipelineTerminal class. Valve class has its own private, protected and public member variables. Similarly, Parent class PipelineTerminal has its own private, protected and public member variables.

class PipelineTerminal { public name: string; protected id: number; private authcode: number; constructor(name: string, id: number, authcode: number) { this.name = name; this.id = id; this.authcode = authcode; } }

When the parent class PipelineTerminal is inherited, only the protected and public member variables are available to its child class Valve. In valve class, private member variable valveauthocode is specific to class valve and hence accessible only within class valve, whereas public member variable valve is accessible anywhere in the program. We use super keyword to refer the parent class member variable.

class Valve extends PipelineTerminal { public valve: string; protected valveid: number; private valveauthcode: number; constructor(id: string, pipename: number, authcode: number, valvename: string, valveid: number, valvecode: number) { super(id, pipename, authcode); this.valve = valvename; this.valveid = valveid; this.valveauthcode = valvecode; }  valveDetails = ():void => { alert(`Valve area details are: ${this.name} ${this.id} ${this.valveid} ${this.valve} ${this.valveauthcode}`); } }

The final section of the program creates object of the class valve. We pass input parameters in class valve and class PipelineTerminal constructors. The class valve reuses pipelineid member variable to display full details of the valve.

let valve = new Valve("CRUDE OIL", 777, 444, "TANK-VALVE", 560, 120); valve.valveDetails();

So we conclude that, we can define scope of the class and its members by declaring them public, private and protected in Typescript and hence we can achieve encapsulation in Typescript.