Collecting Inheritance

Collecting InheritanceIt’s not exactly what you think…Zurich OkorenBlockedUnblockFollowFollowingMay 17Photo by John Schnobrich on UnsplashWe won’t be talking about the money you might be getting when your Great Aunt Gertrude passes away.

Instead we’ll be comparing some differences in behaviors and implementations of inheritance in Object Oriented Programming Languages.

The two we’ll focus on are Python’s class-based inheritance and JavaScript’s prototype-based inheritance.

This article assumes that you are at least familiar with Object Oriented Programming (OOP), but if not Alexander Petkov’s article, How to explain object-oriented programming concepts to a 6-year-old, is a great resource to get you up to speed.

Nothing like the CLASS-icsIn Python inheritance is handled through the use of classes.

Meaning that methods and properties are passed down before the creation of an object.

Take a look at the following block of code:In Python classes behave as you would expect them to according to the standard definition of OOP:Classes are the blueprints to make objects.

Meaning that all the methods and properties in the code block do not exist until an object is created (instantiated).

In Python inheritance is implemented simply by passing a class into another class.

The Runner class in this example is inheriting from the Person class above it.

We can tell because the parenthesis in the line, class Runner(Person): indicate that Person, which is another class, is being passed into the Runner class.

Let’s try out these classes.

Here, we created an instance of the Person class and called it Bob.

Here we can see that Bob has a name, can speak and he can also run.

Then, we instantiated the Runner class and made a Runner object.

We then called it Usain after the famous Olympian.

Notice that even though we didn’t declare a speak method in the Runner class, objects we create from the Runner class can still speak .

This is due to the fact that it inherits everything from the Person class including the speak method and the name property.

Method OverridingYou might notice that both Runner and Person have a run method, but they don’t look the same.

That’s because we’re overriding the Person class’s run with a new run specific to the Runner class.

This means objects created from the Runner class have different run methods than those from Person, because Runner objects will use methods specific to the Runner class, before using methods from their parent class.

Shifting GearsNow that we understand at least some of the basics of inheritance in Python, lets compare that to a very different implementation by JavaScript.

Again,let’s take a look at another block of code:I don’t know about you, but I think there’s something different going on here.

Well, besides the obvious syntactic changes, JavaScript does something fundamentally different to tackle the concept of inheritance.

This is actually utilizing object composition to produce an outcome similar to what you would see in class-based inheritance.

However, instead of passing down methods and properties from classes to sub-classes, object composition allows us to pick and choose components from multiple objects that are not necessarily related and create new objects from those components.

Just to make sure that the above code works, let’s run the following:As you can see, we achieved the same outcome as Python’s class-bases inheritance through JavaScript’s object composition solution.

The key difference is that instead of creating templates for sub-classes based on parent classes, we make functions that take components from other objects and create a new object from those components.

Those functions are called factories, just like real factories, they build the same objects over and over again using the same components.

ConclusionThe OOP ecosystem is quite diverse.

Here we saw that even with inheritance, one of the pillars of OOP, there can be very different solutions to tackle the same problem.

This article was meant to give a surface-level comparison of inheritance in Python and in JavaScript.

Neither one is better than the other, but just know you’re working with very different tools and should be handled differently in order to make the most of either one.

Further ReadingFor the intrepid souls and avaricious learners, here are some articles I found to be very useful in researching for my article:Master the JavaScript Interview: What’s the Difference Between Class & Prototypal Inheritance?“Master the JavaScript Interview” is a series of posts designed to prepare candidates for common questions they are…medium.

comClass Composition in JavaScriptIt seems like "composition over inheritance" is the new motto.

Everyone's talking about it, and that's not strange…alligator.

ioPython InheritanceInheritance is a powerful feature in object oriented programming.

It refers to defining a new class with little or no…www.


comInheritance in Python – GeeksforGeeksInheritance is the capability of one class to derive or inherit the properties from some another class.

The benefits of…www.


org.. More details

Leave a Reply