Consider this TypeScript class with different access modifiers. What will be printed when the code runs?
class Animal { public name: string; private age: number; protected sound: string; constructor(name: string, age: number, sound: string) { this.name = name; this.age = age; this.sound = sound; } public makeSound() { console.log(`${this.name} says ${this.sound}`); } } class Dog extends Animal { public showAge() { // console.log(this.age); // What happens if uncommented? console.log(`Dog's sound is ${this.sound}`); } } const dog = new Dog('Buddy', 5, 'Woof'); dog.makeSound(); dog.showAge();
Remember that protected members are accessible in subclasses, but private members are not.
The public member name is accessible everywhere. The private member age is only accessible inside Animal. The protected member sound is accessible inside Animal and its subclasses like Dog. So dog.showAge() can access sound but not age.
Choose the correct access modifier in TypeScript that restricts access to a class member only inside the class where it is declared.
Think about which modifier hides the member from subclasses and outside code.
private means the member is accessible only inside the class it is declared in. protected allows access in subclasses. public allows access everywhere. readonly is about immutability, not access.
Look at this code snippet. Why does it cause a compilation error?
class Person { private ssn: string; constructor(ssn: string) { this.ssn = ssn; } } const p = new Person('123-45-6789'); console.log(p.ssn);
Check the access level of ssn and where it is accessed.
The ssn property is marked private, so it cannot be accessed outside the Person class. Trying to access p.ssn causes a compilation error.
Choose the correct syntax to declare a protected property id of type number inside a class.
Access modifiers come before the property name and type.
The correct syntax is protected id: number;. Modifiers like private and public cannot be combined with protected. The type comes after the property name.
Given the following TypeScript classes, how many members can be accessed directly from an instance of Employee?
class User { public username: string; private password: string; protected email: string; constructor(username: string, password: string, email: string) { this.username = username; this.password = password; this.email = email; } public getUsername() { return this.username; } } class Employee extends User { public employeeId: number; constructor(username: string, password: string, email: string, employeeId: number) { super(username, password, email); this.employeeId = employeeId; } public getEmail() { return this.email; } } const emp = new Employee('alice', 'secret', 'alice@example.com', 101);
Consider which members are accessible directly on the instance and which are private or protected.
From the instance emp, you can access username (public), employeeId (public), getUsername() (public method), and getEmail() (public method). The password is private and email is protected, so they are not accessible directly. Protected members are accessible inside subclasses but not from outside instances.