Public access modifier
“Public” is the default access modifier that can be accessed from anywhere. We don’t have to explicitly specify a public keyword when declaring a property or a method in Typescript.
class Student {
name: string; //public by default
constructor(name: string) {
this.name = name;
}
}
let student = new Student("Bob");
console.log(student); // Student {name: 'Bob'}
Here, name
property is public by default and it can be accessed from within the class as well as outside the class. You can also modify this property from outside of class like below.
student.name = "john";
student; // Student {name: 'john'}
You can optionally use the public keyword while declaring properties and methods but generally it is not required.
class Student {
public name: string;
constructor(name: string) {
this.name = name;
}
}
let student = new Student("Bob");
console.log(student); // Student {name: 'Bob'}
Similarly, when using methods,
class Student {
public name: string;
constructor(name: string) {
this.name = name;
}
public greet() {
console.log("hello!");
}
}
let student = new Student("Bob");
student.greet(); // hello!
However, we can make use of the public
keyword to reduce the code while declaring classes like the above in an alternate way.
class Student {
// name: string; NOT REQUIRED
constructor(public name: string) {
// initialization NOT REQUIRED
}
greet() {
console.log("hello!");
}
}
let student = new Student("Bob");
console.log(student); // Student {name: 'Bob'}
Private access modifier
With a private access modifier, the properties and methods can only be accessed from within the class. To declare a property or a method as private we need to prefix them with the private
keyword.
class Student {
private name: string;
constructor(name: string) {
this.name = name;
}
}
let student = new Student("Bob");
console.log(student); // Student {name: 'Bob'}
console.log(student.name); // Property 'name' is private and only accessible
//within class 'Student'.
We cannot modify the name
property from outside the class.
student.name = "John"; // Property 'name' is private and only accessible
// within class 'Student'.
You can however access the name
property from within a class.
class Student {
private name: string;
constructor(name: string) {
this.name = name;
}
greet() {
console.log(`hello! ${this.name}`); // allowed
}
}
let student = new Student("Bob");
student.greet(); // hello! Bob
Similarly, you can also declare a method as private.
class Student {
private name: string;
constructor(name: string) {
this.name = name;
}
private greet() {
console.log("hello!");
}
}
let student = new Student("Bob");
student.greet(); // Property 'greet' is private and
// only accessible within class 'Student'.
To access the private greet()
method, we can call it from within the Student class.
class Student {
private name: string;
constructor(name: string) {
this.name = name;
}
private greet() {
console.log("hello!");
}
publicGreet() {
this.greet();
}
}
let student = new Student("Bob");
student.publicGreet(); // hello!
The above can also be shortened as below.
class Student {
// private name: string;
constructor(private name: string) {}
private greet() {
console.log("hello!");
}
publicGreet() {
this.greet();
}
}
let student = new Student("Bob");
student.publicGreet(); // hello!