Unit_11: JS inheritance Flashcards
In JavaScript, what is inherited when a class inherits from another class?
Only the methods are inherited, not the instance variables.
When are instance variables created in JavaScript inheritance?
Instance variables are created when the super constructor is called, i.e., when the code creating them is executed.
in JavaScript, what is required for method overriding?
you only need to use the same method name, not necessarily the same parameters.
Does method overriding in JavaScript require the same number of parameters as the method being overridden?
No, method overriding in JavaScript does not require the same number of parameters; the parameters are essentially a recommendation.
Why can only one version of a method exist in JavaScript?
Because method overloading is not supported in JavaScript, only the last version of a method with the same name is used.
What keyword is used in JavaScript to access the superclass version of a method?
The super keyword.
In JavaScript, what is the purpose of refinement in inheritance?
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.
What is shadowing in the context of JavaScript classes?
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.
Does shadowing happen with public or private fields?
Shadowing happens only with private fields.
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();
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.
What does the instanceof operator do in JavaScript?
It checks if an object is an instance of a specific class or its subclass.
How is instanceof in JavaScript different from Java?
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”).
s1 = new GradStudent(“Roger”, 202, “Thesis”);
if (s1 instanceof Student)
console.log(“s1 is of type Student or a subtype!”);
s1 is of type Student or a subtype!
Does JavaScript have a keyword for declaring abstract classes or methods?
No, JavaScript does not have a built-in keyword for abstract classes or abstract methods. You must simulate them manually.
What are the goals of using an abstract class (even when simulated) in JavaScript?
- Prevent instantiation of the base class.
- Enforce that subclasses implement certain methods.
- Provide shared code that subclasses can inherit.
How can we simulate an abstract class in JavaScript?
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()”);
}
}