PrepAway - Latest Free Exam Questions & Answers

Which two code segments should you use?

You are creating a class named Sedan that must inherit from the Car class. The Sedan class must
modify the inherited fourDoor () method. The Car class is defined as follows.

Future instances of Sedan must be created with the overridden method.
You need to write the code to implement the Sedan class.
Which two code segments should you use? (Each correct answer presents part of the solution.
Choose two.)

PrepAway - Latest Free Exam Questions & Answers

A.
Option A

B.
Option B

C.
Option C

D.
Option D

2 Comments on “Which two code segments should you use?

  1. developer says:

    Parasitic Combination Inheritance

    Combination inheritance is the most often-used pattern for inheritance in JavaScript, though
    it is not without its ineffi ciencies. The most ineffi cient part of the pattern is that the supertype
    constructor is always called twice: once to create the subtype’s prototype, and once inside the
    subtype constructor. Essentially, the subtype prototype ends up with all of the instance properties of
    a supertype object, only to have it overwritten when the subtype constructor executes. Consider the
    combination inheritance example again:

    function SuperType(name){
    this.name = name;
    this.colors = [“red”, “blue”, “green”];
    }

    SuperType.prototype.sayName = function(){
    alert(this.name);
    };

    function SubType(name, age){
    SuperType.call(this, name); //highlighted lines – second call to SuperType()

    this.age = age;
    }

    SubType.prototype = new SuperType(); //highlighted lines – first call to SuperType()
    SubType.prototype.constructor = SubType;
    SubType.prototype.sayAge = function(){
    alert(this.age);
    };

    The highlighted lines of code indicate when SuperType constructor is executed. When this code is
    executed, SubType.prototype ends up with two properties: name and colors. These are instance
    properties for SuperType, but they are now on the SubType’s prototype. When the SubType constructor
    is called, the SuperType constructor is also called, which creates instance properties name and colors
    on the new object that mask the properties on the prototype.

    As you can see, there are two sets of name and colors properties: one on the instance and one on
    the SubType prototype. This is the result of calling the SuperType constructor twice. Fortunately,
    there is a way around this.




    0



    0

Leave a Reply