Gaining Understanding in Computer Science Part 1: Class Inheritance

Joshua Singleton
4 min readMar 8, 2021

--

In my journey into the software engineering field there are a lot of information to digest. On top of that heap of information are opinions for and against any and everything that has to do with programming. As I learn I cannot help but to feel overwhelmed. All I am trying to do is take over the tech industry with my To Do app. You may feel the same way.

It is my mission to not only have a conversational understanding about fundamental topics of computer science but to also put that understanding into practical use. Some say that when you look for answers to your questions you come away with more questions; I am comfortable with that. However, it is always beneficial to gain understanding along the way. As I strive to gain that understanding I hope that you will progress in insight as well.

What is Inheritance in JavaScript?

Class inheritance can be found in programming languages such as Ruby, Java and Javascript. It is also used in libraries such as React. The following syntax may be familiar to those who have/are using React: “class Index extends React.Component …”

Example of Class Inheritance:

Class Inheritance with functions

With class Inheritance, you are able to create instances of that class and dynamically modify that instance to your personal touch…

In the example above I created a Clothes class. This class has a built in function that I created called “dressingUp”. When you invoke this function on an instance of the Clothes class it will return a string that takes in the brand that you put in the argument of a new instance of the Clothes class. What is awesome about Class inheritance is capability for being predictable and set a standard while being customizable to the particular instance of that class. LinkedIn and every other social platform has a standard design. Whether it is myLinkedIn page or yours, both our pages inherits the same structure with the freedom of adding our personal touch.

Moreover, we can build on our Clothes class with an Attire class. This allows us to “extend” functions in our original Clothes class along with additional functions in our Attire class.

What is super and how is it used?

The super keyword refers to superclass (parent) objects. It is used to call super class methods, and to access the superclass constructor. The most common use of the super keyword is to eliminate the confusion between super classes and subclasses that have methods with the same name according to W3Schools.com

What you end up with is the ability to create new instances of classes and add your own spin to it. Check out my example below! I am able to assign a variable to a new instance of the Clothes class and pass in a brand in the argument. This allows me to invoke the dressingUp() function and print out a customizable phrase with the “Nike” string passed in the argument.

In addition to that. I can create a new instance of my Attire class, pass in my brand and accessories in the argument and invoke my Clothes and Attires functions on my instances. Think about the ways that this can come in handy in your programming experience…

The Push back on Class Inheritance

Some scholars and practitioners in the software engineering field consider using super in class inheritance useless if not dangerous. Popular opinion on StackOverflow consider super redundant if not passed with an explicit argument.

You also have the Gorilla and the Banana problem. This analogy is greatly demonstrated in Eric Elliot’s blog on The Two Pillars of JavaScript. Essentially, if you want an instance of your class to have just one aspect of the blueprint then you have to accept everything that is offered in that blueprint. You want a banana? Then you need to take the Gorilla that comes with it. On the other hand if you want an instance to have different aspects other than your blueprint then you need to create another blueprint.

“The problem with object-oriented languages is they’ve got all this implicit environment that they carry around with them. You wanted a banana but what you got was a gorilla holding the banana and the entire jungle.” ~ Joe Armstrong

A Screw Loose in New

There are many discussions against and for invoking new instances of classes. One of the pushbacks is that you cannot make simple changes in your Class model without damaging most of your dependent code that relies on that model. Say for instance in my example, I changed my mind and instead of naming my function dressingUp() I called it puttingOnClothes(). That means that whenever I invoked the function dressingUp() it will no longer work. That creates a nice time of debugging that you may not be willing or have the time to do.

Programmers have created solutions to mitigate these problems. A great solution is object composition. As stated in the “Design Patterns” book by the Gang of four in one of their foundational principles: “favor object composition over class inheritance.”

I hope that you have gained a better understanding into Class Inheritance. My journey in this knowledge is still growing and I hope yours is as well. Connect, Follow and reach out to me. I would love to hear your feedback and produce answers to the questions that may arise…

--

--

Joshua Singleton
Joshua Singleton

Written by Joshua Singleton

Student of Code in the hopes of creating a new social network to enhance the community!

No responses yet