Trying to understand composition vs. inheritance

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”.

Composition: https://en.wikipedia.org/wiki/Has-a
Inheritance: https://en.wikipedia.org/wiki/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.

I’m actually confused why you’re asking this on FCC because JavaScript doesn’t conform to this kind of object-oriented programming at all and its “Objects” are nothing like traditional class-based Objects like you would find in C++, Java, or Python. But that’s basically what the concepts mean.

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.

I think this is more difficult for those of us whose programming experience is mostly, or even entirely, in JavaScript because we never have to actually deal with class inheritance, and in particular, single class inheritance, which is where many of the problems with OOP come up. Multiple inheritance, which is present in Java, C++, C#, and Python (to name a few), is much more flexible, but definitely brings us to the “5 course dinner” problem. This can make code more difficult to maintain and debug because classes contain state as well as functionality, and stuffing your new class with a bunch of state it doesn’t need is a recipe for disaster.

Your definitions certainly get to the main differences between composition and inheritance, but I think to truly “get it”, you should take the time to learn a fundamentally object oriented language. Java is very accessible, and Java 8 in particular has some very JavaScript-like syntax that should be very familiar (I’m thinking of lambdas in particular). I very much doubt that you’ll run into the sorts of world-ending problems that Mr. Elliot talks about, but it will very much give you a sense of what could go wrong if your inheritance chain gets out of control.

1 Like