0
0
Typescriptprogramming~20 mins

Access modifiers public private protected in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Access Modifier Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this TypeScript code using access modifiers?

Consider this TypeScript class with different access modifiers. What will be printed when the code runs?

Typescript
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();
ABuddy says Woof\nDog's sound is Woof
BBuddy says Woof\nError: Property 'sound' is private and only accessible within class 'Animal'.
CError: Property 'age' is private and only accessible within class 'Animal'.
DBuddy says Woof\nDog's sound is undefined
Attempts:
2 left
💡 Hint

Remember that protected members are accessible in subclasses, but private members are not.

🧠 Conceptual
intermediate
1:00remaining
Which access modifier allows a class member to be accessed only within the class itself?

Choose the correct access modifier in TypeScript that restricts access to a class member only inside the class where it is declared.

Apublic
Bprivate
Cprotected
Dreadonly
Attempts:
2 left
💡 Hint

Think about which modifier hides the member from subclasses and outside code.

🔧 Debug
advanced
1:30remaining
Why does this TypeScript code cause an error?

Look at this code snippet. Why does it cause a compilation error?

Typescript
class Person {
  private ssn: string;

  constructor(ssn: string) {
    this.ssn = ssn;
  }
}

const p = new Person('123-45-6789');
console.log(p.ssn);
AError because 'ssn' is protected and cannot be accessed here.
BError because 'ssn' is not initialized in the constructor.
CNo error; it prints '123-45-6789'.
DError because 'ssn' is private and cannot be accessed outside the class.
Attempts:
2 left
💡 Hint

Check the access level of ssn and where it is accessed.

📝 Syntax
advanced
1:00remaining
Which option correctly declares a protected member in a TypeScript class?

Choose the correct syntax to declare a protected property id of type number inside a class.

Apublic protected id: number;
Bprivate protected id: number;
Cprotected id: number;
Did: protected number;
Attempts:
2 left
💡 Hint

Access modifiers come before the property name and type.

🚀 Application
expert
2:30remaining
How many accessible members does this subclass instance have?

Given the following TypeScript classes, how many members can be accessed directly from an instance of Employee?

Typescript
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);
A4 (username, employeeId, getUsername, getEmail)
B3 (username, employeeId, getUsername)
C2 (employeeId, getEmail)
D5 (username, password, email, employeeId, getUsername)
Attempts:
2 left
💡 Hint

Consider which members are accessible directly on the instance and which are private or protected.