2. Inheritance in TypeScript Flashcards

1
Q

How do you enforce a class to extend another class in TypeScript?

A

By using the extends keyword on a new class to give it a base class.

eg. class Dog extends Animal { //…

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

How do you extend the functionality or data of a base class from a derived class?

A

By simply declaring new methods or properties for the derived class. The derived class will then inherit all the functionality and data of the base class but also provide the additional ones defined in it.

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

How do you enforce a class to implement another class in TypeScript?

A

By using the implements keyword.

eg class aClass implements anotherClass { //…

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

What’s the difference between extending and implementing a class?

A

Extending a base class from a derived class means that there is a direct inheritance from the former to the latter, meaning that it adopts its methods and properties. Implementing, on the other hand, only ensures that the base class’s “shape” is implemented on the derived class. Meaning it has the same properties/methods as its base class, but it implements them itself rather than adopting them.

In other words, extends defines a “child” whereas implements enforces shape.

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

How does a derived class reference its base class?

A

By using the super keyword within its constructor or methods.

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

How do you determine whether an object is derived from a given base class?

A

By using the instanceof keyword.

eg. dog instanceof animal;

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

Is a class that implements another an instance of the class it implements?

A

No.

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

Is a class that extends another an instance of the class it extends?

A

Yes.

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

How does a derived class use the constructor of its base class?

A

By using the super keyword with parentheses and passing it the parameters that the base class constructor requires.

eg. super(“alican”, “demirtas”);

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

How do you access a base class’s methods from its derived classes?

A

By dotting into the super keyword and calling the method.

eg. super.meow();

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

What happens when a derived class overrides a base class’s method and then tries to use that method by dotting into the super keyword?

A

It calls its own method rather than the base class’s unless it’s in the method definition that overrides the same-named method. This is because once you override something, it’s permanent.

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

Should you use any over generics or is it the other way around?

A

The other way around. Generics are a much more robust approach to giving static typing to a dynamic type.

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

How do you make a class generic in TypeScript?

A

By using angle brackets to define a type parameter. Once this is done, we can use that type all over the class.

eg. class User {
   classicUserData:T;
...
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is method/parameter overriding?

A

It’s when a child class overrides its parent class’s methods.

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

How do you overload a method in TypeScript?

A

You can’t. You can only override it using the exact same parameters that the base method has. One way to work around this, though, is to have optional parameters.

eg.
public hasAccess(user:User = undefined) { //...
How well did you know this?
1
Not at all
2
3
4
5
Perfectly