Introduction
Access modifiers are keywords used to specify the declared accessibility of a member or a type.
Let's discuss how access modifiers i.e. variables and methods are declared and accessed as Private, Public and Privileged in JavaScript.
What is Access Modifiers?
An access modifier is a keyword that we can apply to a member of a class to control its access from the outside.
The followings are the access modifiers in most of the object-oriented programs.
public
, private
and protected
.
Public Access Modifiers
By default, all members (properties, fields, methods, or functions) of classes are public accessible from outside the class.
For example:
1 2class Employee 3{ 4 5 constructor(name: string) 6 { 7 this.employeeName = name 8 } 9 10 greet() {11 console.log(`Good Morning ${this.employeeName}`)12 }13 14}15 16let employee = new Employee('Ossama')17 18employee.greet()
Explanation:
-
We Define a public property called
employeeName
, and it's accessible from outside the class. -
make a
constructor
with parameter has default type ofstring
-
make a
greet
method to say Good Morning to the employee :)
After that, we define a variable to store the new instance of the Employee
class on it, and call the greet
method after that.
Output:
Good Morning Ossama
Private Access Modifiers
Private members can not accessible from outside the class, and it can access only internally within the class.
Let's add a private method and try to access it outside the class
1class Employee 2{ 3 constructor(name: string) 4 { 5 this.employeeName = name 6 } 7 8 greet() { 9 console.log(`Good Morning ${this.employeeName}`)10 }11 12 private convertToString(num: number) {13 return num.toString()14 }15}16 17let employee = new Employee('Ossama')18 19employee.convertToString(20)
Explanation:
In this case we're adding a private method called convertToString
and its role is simply converted the number to string type. We tried to access it outside the class and here is the output:
1(method) Employee.convertToString(num:number): string2property 'convertToString' is private and only accessible within class 'Employee'
As you can see you can access the private (methods, properties ...) just inside the class and not outside.
protected Access Modifiers
Protected members are accessible only internally within the class or any class that extends it, but not externally.
Meaning, you can access protected members only inside the class or when you extended the class
Let's take a look at a practical example:
1class Employee 2{ 3 public employeeName: string 4 5 constructor(name: string) 6 { 7 this.employeeName = name 8 } 9 10 protected greet() {11 console.log(`Good Morning ${this.employeeName}`)12 }13}
here if you're trying to access greet
method outside the class like this:
1let employee = new Employee('ossama')2employee.greet()
We can't and the compiler throw an error
1(method) Employee.greet(): void2Property 'greet' is protected and only accessible within class 'Employee' and its subclasses
So, in this case, we have two options to access the greet
method:
- within the class itself
- within its subClasses
Let's see an example at the second option:
1class Employee 2{ 3 public employeeName: string 4 5 constructor(name: string) 6 { 7 this.employeeName = name 8 } 9 10 protected greet() {11 console.log(`Good Morning ${this.employeeName}`)12 }13}14 15class Manager extends Employee16{17 public managerName: string18 19 constructor(managerName: string) {20 super(managerName) // super refer to the Employee constructor21 }22 23 managerGreet() {24 this.greet()25 }26}27 28let manager = new Manager('Ossama')29 30manager.greet()31 32manager.managerGreet()
Explanation:
In this case we create a new class extends the Employee
class. When you extend a class it called inheritance, which mean you are borrowing its (properties, methods, ...etc.)
So, is this situation:
-
You can't access
greet
directly fromManager
class because it's protected method and inherited fromEmployee
class and the method still protected. -
You can access the
greet
method fromManagerGreet
Because it's a public method and thegreet
protected method inside it, and this is the point from the example.
Conclusion
In this article we take a look at the access modifier public, private and protected.
We can summarize everything in:
- public: when you want to access the members everywhere in your application.
- private: when you want to access the members only inside the class.
- protected: when you want to access the members inside the class and its subclasses.