3. Access Modifiers Flashcards

1
Q

What are the three major access modifiers that help with encapsulation in TypeScript?

A

private, protected, and public.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does the private access modifier do?

A

It makes a member accessible only within the class to which it belongs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does the protected access modifier do?

A

It makes a member accessible only within the class to which it belongs and the classes that derive from it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does the public access modifier do?

A

It makes a member accessible from anywhere where the class itself is accessible.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Where is an access modifier written?

A

In front of a class member.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do the default access modifiers differ from TypeScript to C#?

A

For the former, the default access modifier is public, whereas, for the latter, it is private.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What happens when a class that implements another overrides a protected member of the class it implements?

A

Compiler error. Protected members can only be accessed by child classes. Implementation doesn’t create child classes, extension does.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does the static access modifier do?

A

It makes a member accessible only through an invocation of the class itself, rather than an instance thereof.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you access the instance of a class from its static methods?

A

You can’t. Static methods don’t work with instances, and therefore there is no object in question.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you define a private static method?

A

You can’t. Static methods in TypeScript can only be public.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Is it possible to define two methods with the same name, one being static and the other an instance method, inside the same class?

A

Yes, because they are never going to be invoked in the same way.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does the readonly access modifier do?

A

It allows us to only read the value of a property, not modify it. It also disallows us to override it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Can you set a read-only property from within its class’s constructor?

A

Yes. Yes, you can.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly