Public, private, and protected modifiers
Understanding public
class Employee {
public id: number;
public firstName: string;
public lastName: string;
constructor(id: number, firstName: string, lastName: string) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public getFullName() {
return this.firstName + ' ' + this.lastName;
}
}
// create Employee class object
let employee = new Employee(100, 'Ramesh', 'Fadatare');
console.log(employee);
console.log(employee.getFullName());
Employee { id: 100, firstName: 'Ramesh', lastName: 'Fadatare' }
Ramesh Fadatare
var Employee = /** @class */ (function () {
function Employee(id, firstName, lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
Employee.prototype.getFullName = function () {
return this.firstName + ' ' + this.lastName;
};
return Employee;
}());
// create Employee class object
var employee = new Employee(100, 'Ramesh', 'Fadatare');
console.log(employee);
console.log(employee.getFullName())
Understanding private
class Employee {
private id: number;
public firstName: string;
public lastName: string;
public getFullName() {
return this.firstName + ' ' + this.lastName;
}
}
// create Employee class object
let employee = new Employee();
employee.id = 100; // Property 'id' is private and only accessible within class 'Employee'.ts
employee.firstName = 'Ramesh';
console.log(employee);
console.log(employee.getFullName());
Property 'id' is private and only accessible within class 'Employee'.ts
Understanding protected
class Person {
protected name: string;
constructor(name: string) {
this.name = name;
}
}
class Employee extends Person {
private department: string;
constructor(name: string, department: string) {
super(name);
this.department = department;
}
public getElevatorPitch() {
return `Hello, my name is ${this.name} and I work in ${this.department}.`;
}
}
let howard = new Employee("Howard", "Sales");
console.log(howard.getElevatorPitch());
console.log(howard.name); // Property 'name' is protected and only
// accessible within class 'Person' and its subclasses.
Property 'name' is protected and only accessible within class 'Person' and its subclasses.
class Person {
protected name: string;
protected constructor(theName: string) { this.name = theName; }
}
// Employee can extend Person
class Employee extends Person {
private department: string;
constructor(name: string, department: string) {
super(name);
this.department = department;
}
public getElevatorPitch() {
return `Hello, my name is ${this.name} and I work in ${this.department}.`;
}
}
let howard = new Employee("Howard", "Sales");
let john = new Person("John"); // Error: The 'Person' constructor is protected
Understanding readonly modifier
class Employee{
readonly id: number;
public firstName: string;
readonly lastName: string;
constructor(id: number, firstName: string, lastName: string){
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public getFullName(){
return this.firstName + ' ' + this.lastName;
}
}
// create Employee class object
let employee = new Employee(100, 'Ramesh', 'Fadatare');
employee.id = 200; // Error: Cannot assign to 'id' because it is a read-only property.
employee.lastName = 'Kapoor'; // Error: Cannot assign to 'lastName' because it is a read-only property
console.log(employee);
console.log(employee.getFullName());
employee.id = 200; // Error: Cannot assign to 'id' because it is a read-only property.
employee.lastName = 'Kapoor'; // Error: Cannot assign to 'lastName' because it is a read-only property
Learn more about TypeScript at TypeScript Tutorial with Examples.
Continue Learning with Java Guides
Want to take your programming skills to the next level? Explore my practical Udemy courses on Java, Spring Boot, Microservices, Full-Stack Development, Docker, and Generative AI. You can also follow my structured learning roadmaps to become a professional developer.
By Ramesh Fadatare - Udemy Instructor Partner
Comments
Post a Comment
Leave Comment