📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
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.
Comments
Post a Comment
Leave Comment