In object-oriented programming, a class is a blueprint for creating objects (a particular data structure), providing initial values for state (member variables or attributes), and implementations of behavior (member functions or methods).
Inheritance describes a certain form of code organization and architecture — a way of modeling real world problem domains in our software. One class can inherit all of the properties of another class.A class that is used as the basis for inheritance is called a superclass or base class. A class that inherits from a superclass is called a subclass or derived class. The level of nesting subclasses is not limited in most programming languages.
It's important to understand that class is a descriptor of new type, class is the one that inherits properties and methods from another class, not object that is instantiated from that class. So when you instantiate the subclass, the object that is constructed contains all the properties and methods from all classes above it , and also has its own properties and methods.
Let's take a look at this pseudocode example and see how memory would look if code is executed in some OOP language (like Java or C#).
At this point in time we do not have any objects in memory, we have only defined our classes. Let's instantiate all of the 3 classes and see how memory looks.
This is of course simplified model of how memory would look like, there would be couple more things in memory, depending on the language, but that's not the point and we won't talk about that now. What we wanted to see here is that inheritance is an action of copying. That fact is really important!
So, when class B inherits A line is executed, what will happen actually is that all properties that the definition of class A has, will be copied to class B. Later when you instantiate class B , constructed object will contain everything that is defined in class B, which is, all of the class A properties plus class B specific properties.
What we did here is we connected objectB with objectA. So now object A is a prototype of object B.
Most of the JS developers will tell you that in this case object B inherits everything from object A.
And it may be tempting to think like that, especially because of the fact that when you type B.nameA you will actually get "Objectc A" string value back. But, let's take a look at our memory and see what's in there.
As we can see , object B has only one property (his own), and nothing was copied over from object A. So is this inheritance then ? Can we say that object B "inherited" properties from object A ? I think not. What object B has is a connection to object A via prototype, and a way to ask for and get properties that it doesn't have. That 'way of asking' is often referred as "lookup algorithm".
Much better name for it would be Prototypal Delegation ! Because when some object is asked to retrieve some property value, if it doesn't have that property, it will always delegate the task of retrieving that property value to its prototype.
On the other side, delegation based languages are more memory efficient since objects are containing only their own properties, and not all of the properties from theirs super classes.
The main difference is that in OOP , inheritance is the action of copying. One class copies everything from some other (base) class. In JS, we do not have classes, and inheritance is not action of copying, it is actually action of delegation. We have means to connect two objects together, so that one object can delegate its tasks(property value retrieval) to another object.