Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly does a derived class inherit from its base class?

I currently am learning ES6 classes for Javascript and I seem to understand the concept of them but I don't understand what a derived class inherits from its base class. Is it the methods of the class? Is it safe to assume that the methods of a class are properties of said class? In which case, they are part of the prototype and are thus inherited by objects down the prototype chain. And what about the constructor? Are properties defined inside the constructor inherited?

Thank you for your consideration!

like image 344
Whiteclaws Avatar asked Jan 17 '20 19:01

Whiteclaws


People also ask

What does a derived class inherit from its base class quizlet?

A derived class can effect state changes in base class private members only through public, protected, internal methods provided in the base class and inherited into the derived class. A class that inherits from another class is referred to as the derived class.

What does a derived class inherit from the base class Mcq?

Which among the following is inherited by a derived class from base class? Explanation: The class inheriting another class, inherits all the data members and member functions that are not private. This is done to ensure the security features with maximum flexibility.

What does derived class does not inherit from the base class?

Following are the properties which a derived class doesn't inherit from its parent class : 1) The base class's constructors and destructor. 2) The base class's friend functions. 3) Overloaded operators of the base class.

What are derived classes also called?

A derived class is also called a: Answers: sub class. super class. base class. All of these. can not be accessed by name in any other class (that is, other than classes named in A-C.). A method or instance variable modified by protected: Answers: can not be accessed by name inside its own class definitions.

Which are inherited by the derived class in C++?

Clarification: Both data members and member functions are inherited by derived class in C++. 4. What will be the output of the following C++ code? Clarification: In this program, We are passing the value and manipulating by using the derived class.

What is base class in Java?

The Base Class, also known as the Parent Class or the Super Class is a class, from which other classes are derived. In other term it is a base class for other derived classes. That means if a derived class which inherits the base class has all members of a base class as well as can also have some additional properties.

How does a constructor for a derived class begin?

A constructor for a derived class begins with an invocation of a constructor for the base class. Answers: True False False A derived class contains only public instance variables and public methods from the base class.


1 Answers

Classes are more or less just syntactic sugar for setting up prototype inheritance.

class Derived extends Base {}

is equivalent to

function Derived(...args) {
  return Base.apply(this, args);
}

Object.setPrototypeOf(Derived, Base);
Object.setPrototypeOf(Derived.prototype, Base.prototype);

There is some "magic" involved with super and in the future with public and private class fields, but the basic relationship between objects is the same.

Is it safe to assume that the methods of a class are properties of said class? In which case, they are part of the prototype and are thus inherited by objects down the prototype chain.

Yes, methods become properties of the corresponding prototype object, from which all instances inherit. I.e.

class Foo {
  bar() {}
}

is equivalent to

function Foo() {}
Foo.prototype.bar = function() {}

And since a "base class'" property object is in the prototype chain of the derived class, all its methods are available to instances of the derived class.

Are properties defined inside the constructor inherited?

"Inherited" is the wrong word here. The properties are created on the instance itself since that's how constructors work.

Consider the process to be like this:

// Create new instance
var newInstance = Object.create(Derived.prototype);

// Call base constructor with `this` set to new instance
Base.apply(newInstance);

// Call derived constructor with `this` set to new instance
Derived.apply(newInstance);

If the base constructor contained something like this.base = 42;, then that property would be directly created on the new instance, since this refers to the new instance.

Note: In reality the exact flow is a bit different due to the fact extending built-in classes such as Array need special treatment but the end result is roughly the same.


You didn't ask about static methods but these are still part of the inheritance. static methods become properties of the constructor function itself.

class Foo {
  static bar() {}
}

is equivalent to

function Foo() {}
Foo.bar = function() {}

Because the constructor of the base class becomes the prototype of the constructor of the derived class, all properties defined on the base constructor are available to the derived constructor.


The developer tools in your browser can actually show you all of this:

class Base {

  static staticBase() {}
  
  constructor() {
    this.base = 42;
  }
  
  fromBase() {}
}

class Derived extends Base {

  static staticDervied() {}
    
  constructor() {
    super(); // necessary since we extend base
    this.derived = 21;
  }
  
  fromDerived() {}
}

console.dir(Derived);
console.dir(new Derived());

enter image description here

like image 157
Felix Kling Avatar answered Oct 16 '22 23:10

Felix Kling