It’s taken me about 2 years to even get to the point where this phrase is starting to make any sense at all. For those that “get” it, is this analogous?
Composition: create exactly what you need for your code like choosing items from a “chinese menu”.
Inheritance: You create objects based on other objects that are the closest thing to what you need, but it comes with other things you don’t want… like ordering the “5 course dinner” when you only want the main dish and dessert.
I’m really trying hard to “get” this. Thanks for any confirmation or clarification.
There’s a short phrase that goes with each concept—composition means “has-a” and inheritance means “is-a”.
I definitely understand if that might not help you much right now, but what that means in plain English is that with composition, one class is a component of another; but with inheritance, one class is another but with added functionality.
If you take for example the concept of a Vehicle class, you might have related classes for the parts it contains—the body, engine, tires, windows, etc. These separate classes are all related to the Vehicle class through composition, and it can be said that the Vehicle “has a” body, engine, tire, window, etc. And when you create classes for types of vehicles, like Sedans, SUVs, Trucks, etc, those classes would be related to Vehicle through inheritance, because they all have a body, engine, tires, windows, etc. And in that case, you can say a Sedan “is a” Vehicle, or a Truck “is a” Vehicle. So that explains where those phrases come from.
Thanks for the explanation. ES6 includes classes and I’m trying to understand how classes relate to objects, which led me to a bunch of Eric Elliot stuff (again), which led to this article (again)
My short term goal is to finish the react rogue project by writing a binary space partitioning routine, and I’ve been watching tutorials…some are in ES6 (classes) , some in ES5 (objects)
I’m just trying to understand.