Typescript Access Modifiers

  • By Pooja Ghodekar
  • November 7, 2023
  • JavaScript
Typescript Access Modifiers

Typescript Access Modifiers

Master TypeScript Access Modifiers to enhance code security and maintainability. Learn public, private, and protected modifiers for cleaner, safer development.

  • typescript use access modifiers  
  • we can use it to control the visibility of the data member of the class  
  • typescript sets the public access modifier by default to all class member  
  • it gives direct access control to the class data member.  
  • these class member are functions and properties we can use class members inside its own class,  anywhere  
  • ouside the class, or within its class or derived class.  
  • the access modifier increases the security of the class member and helps them from invalid use. 

 

  • Public  

In TypeScript by dereliction, all the members( parcels and styles) of a class are public. So, there’s no need to prefix members with this keyword. We can pierce this data member anywhere without any restriction. 

illustration  

class Pupil{  

public studCode number;  

studName string;  

let superstud = new Pupil();  

= 101;  

= ” Joe Root”;  

(stud.studCode””stud.studName);  

In the below illustration, studCode is public, and studName is declared without a modifier, so TypeScript treats them as public by dereliction. Since data members are public, they can be penetrated outside of the class using an object of the class. Elevate your coding skills with Reactjs classes in Pune. Master this powerful library for building dynamic web applications. Enroll now for a brighter coming future!

 

  • Private  

It ensures that the class members are visible only to the class in which it’s contained.  The private access modifier can not be accessible outside of its containing class.  

class Pupil{  

public studCode number;  

private studName string;  

constructor( law number, name string){ 

this.studCode = law;  

= name;  

public display(){  

return( My unique law${this.studCode}, my name${this.studName}.);  

let pupil Pupil = new Pupil( 1,” JoeRoot”);  

console.log(student.display());  

In the below example, studCode is private, and studName is declared without a modifier, so TypeScript treats it as public by default. However, it’ll give a compile error, If we access the private member outside  of the class.  

 

For Free Demo classes Call: 020-71173125

Registration Link: Click Here!

 

  • Protected  

We can not access it from the outside of a class in which it’s containing.  

A Protected access modifier can be accessed only within the class and its class.  

illustration  

class Student{  

public studCode number;  

protected studName string;  

constructor( code number, name string){  

this.studCode = code; 

= name;  

class Person extends Student{  

private department string;  

constructor( code:number, name:string, department :string){  

super( code, name);  

this.department = department;  

public getElevatorPitch(){  

return( My unique code${this.studCode}, my name${this.studName} and I’m in${this.department}  Branch.);  

let joeRoot Person = new Person( 1,” JoeRoot”,” CS”);  

console.log(joeRoot.getElevatorPitch());  

 

illustration 2  

class Stduent{  

public studCOde number;  

private stuNAmestring  

defended marksnumber;  

constructor( stuNAmestring, marksnumber, studCOdenumber){ 

= stuNAme;  

= marks;  

= studCOde;  

public display(){  

return${this.studCOde},${this.stuNAme},${this.marks};  

class person extends Stduent{  

private departmetstring;  

constructor( namestring, codenumber, marksnumber, departmentstring)  {  

super( name, marks, law)  

this.departmet = department;  

public getval(){ 

return${this.studCOde},${this.departmet},${this.marks};  

/ marks is defended and stCode is public so this can accessible directly in subclass  / but stuName is private so it doesn’t allow to accessible in subclass  

let obj1 = new Stduent(” meena”, 33,50);  

obj1.studCOde = 1111;  

/obj1.stuNAme = ” meena”;// private not accessible outside class  /obj1.marks = 88; protectec extrernally not accessible  

(obj1.display())  

/ crete child class( subclass)  

let p1 = new person(” teena”, 33,60,” it”);  

console.log(p1.getval()); 

/ public and protected member are accessible with in the class  

====================================  

For Free Demo classes Call: 020-71173125

Registration Link: Click Here!

tyepscript Accessor  

in ts, the accessor property give a method to access and set the class member.  it has two methods  

  1. getter  
  2. setter  

the getter accessor property is the traditional system which is used for retriving  the value of a variable.  

in object nonfictional, the getter property denote by” get” keyword  it can be public, private, and defended  

get prodName(){  

/ geter, the law execute on gettingobj.prodName  

illustration  

class Shape{  

lennumber = 20; 

breadthnumber = 15;  

get reactangle()  

returnthis.len *this.breadth;  

( new Shape(). reactangle);  

=============================================================  

The setter Accesor property is the traditional system that is used for streamlining the value of a variable. in object literal, the setter property is denote by” set” keyword  

set prodName( value){  

/ setetr, the code execute on settingobj.prodName = value  

illustration 1 

class Hand{  

private, fullNamestring;  

get FullName() string{  

returnthis._fullName;  

set FullName( newNamestring){  = newName  

let e1 = new hand();  

= ” meena ROy”;  

(e1.FullName); 

example2  

class Shape{  

lennumber = 20;  

breadthnumber = 15;  

get reactangle()  

returnthis.len *this.breadth;  

set display( valuenumber){  = value;  

(this.len);  

( new Shape(). reactangle); 

new Shape(). display = 222;  

—————————————— 

For Free Demo classes Call: 020-71173125

Registration Link: Click Here!

 

  • Readonly Modifier  

This modifier needs to be initialized at their protestation time or in the constructor.  

We can also access readonly member from the outside of a class, but its value can not be changed.  illustration  

class Company{  

readonly, country string = ” India”;  

readonly name string;  

constructor( contName string){  

= contName;  

showDetails(){  

(this.name””this._country);  

let comp = new Company(” typescript”);  

(); 

= ” infosys”;// Error, name can be initialized only within constructor  

The getter and Setter giev us a way of having finer control over how a member  is penetrated on each object  

the ts accessors need us to set the compiler to output ECMAscipt 5 or highter.  it doesn’t support below ECMAScript 5.  

the accessor which has a get property without any set property is bydefault assumed to be  read- only.  

Naming convetion for getter and setter  

getXX() and setXX()  

private string name;  

public void setName( string namr){} 

public progeny string getName(){ return name}

Do watch our Components Of Angular video on YouTube.

Author:-

Pooja Ghodekar

Call the Trainer and Book your free demo Class For Mern Stack Call now!!!
| SevenMentor Pvt Ltd.

© Copyright 2021 | SevenMentor Pvt Ltd.

Submit Comment

Your email address will not be published. Required fields are marked *

*
*