Unit_11: JS inheritance Flashcards

1
Q

In JavaScript, what is inherited when a class inherits from another class?

A

Only the methods are inherited, not the instance variables.

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

When are instance variables created in JavaScript inheritance?

A

Instance variables are created when the super constructor is called, i.e., when the code creating them is executed.

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

in JavaScript, what is required for method overriding?

A

you only need to use the same method name, not necessarily the same parameters.

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

Does method overriding in JavaScript require the same number of parameters as the method being overridden?

A

No, method overriding in JavaScript does not require the same number of parameters; the parameters are essentially a recommendation.

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

Why can only one version of a method exist in JavaScript?

A

Because method overloading is not supported in JavaScript, only the last version of a method with the same name is used.

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

What keyword is used in JavaScript to access the superclass version of a method?

A

The super keyword.

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

In JavaScript, what is the purpose of refinement in inheritance?

A

allows a subclass to extend or enhance a method from the superclass by calling the superclass’s version (with super.methodName()) and then adding additional behavior.

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

What is shadowing in the context of JavaScript classes?

A

Shadowing occurs when a subclass defines a private field with the same name as a private field in the superclass. Each class keeps its own separate version of that field.

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

Does shadowing happen with public or private fields?

A

Shadowing happens only with private fields.

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

Why this program print 2 output for the same variable?
class A {
#myField;
constructor() {
this.#myField = “Hi”;
}

print() {
console.log(“A: “ + this.#myField);
}
}

class B extends A {
#myField;
constructor() {
super();
this.#myField = 25;
}

print() {
console.log(“B: “ + this.#myField);
super.print();
}
}

function main() {
let bVar = new B();
bVar.print();
// Output:
// B: 25
// A: Hi
}

main();

A

Because B and A each have their own private #myField, so B’s print() accesses its own field, while super.print() accesses the superclass’s field.

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

What does the instanceof operator do in JavaScript?

A

It checks if an object is an instance of a specific class or its subclass.

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

How is instanceof in JavaScript different from Java?

A

In JavaScript, you rarely need instanceof because there’s no need to cast variables. What matters more is whether the object has the method you need (i.e., “duck typing”).

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

s1 = new GradStudent(“Roger”, 202, “Thesis”);
if (s1 instanceof Student)
console.log(“s1 is of type Student or a subtype!”);

A

s1 is of type Student or a subtype!

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

Does JavaScript have a keyword for declaring abstract classes or methods?

A

No, JavaScript does not have a built-in keyword for abstract classes or abstract methods. You must simulate them manually.

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

What are the goals of using an abstract class (even when simulated) in JavaScript?

A
  • Prevent instantiation of the base class.
  • Enforce that subclasses implement certain methods.
  • Provide shared code that subclasses can inherit.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How can we simulate an abstract class in JavaScript?

A

By throwing an error in the constructor if an attempt is made to instantiate the class directly, and by defining methods that throw errors unless overridden.

class AbstractClass {
constructor() {
if (new.target === AbstractClass) {
throw new Error(“Cannot instantiate abstract class”);
}
}

requiredMethod() {
throw new Error(“Subclass must implement requiredMethod()”);
}
}