What’s so bad about inheritance?

Vinay Ganesh
Jul 30, 2017 · 2 min read

While writing new software, you will have to design how the different classes interact with each other. Just like how we were taught in school to follow object oriented programming(OOP) with inheritance being the key part of OOP, we wrote object oriented code even though technically speaking Inheritance violates encapsulation. This article will talk mostly about what are the disadvantages of inheritance and how to overcome these disadvantages and all in all try to avoid inheritance as much as possible for scalable and feature changing code.

In the code above, there are two classes Downloader and ImageDownloader. The ImageDownloader class inherits from Downloader class and it defines its own version of the download functionality. So the ImageDownloader instance can call download() of Downloader class and download() of its own. The output of the above gist is shown in the comments in the gist. Where is encapsulation? By definition, encapsulation is where the internal representation of an object is generally hidden from the outside world. You just lost encapsulation as soon as you extended the class becuase the derived class knows the internal details of the base class.

As Shown above, lets say you add another function downloadPNG() to ImageDownloader which is a specialized download function that just downloads PNG files. At this point, if you want to add another function called downloadPNG() in the Downloader class which is the base class of ImageDownloader class, you will see a compilation issue. Why? because in inheritance, the derived class is supposed to know about the base class but the base class doesn’t know anything about the derived class. In this trivial example, you would just create a downloadPNG() function in the Downloader class and just prefix an override keyword to the downloadPNG() function of the ImageDownloader class. But when you are developing a bigger application with a large number of classes , you will find yourself in a mess trying to patch the issues created by the hierarchy.

In the above example, lets say you would want to add performChangesAndDownload() function to the Downloader class. Now since ImageDownloader inherits from Downloader, it is unnecessarily getting access to the performChangesAndDownload() function. So the ImageDownloader knows more than what it is suppose to and the user of ImageDownloader can now change something in the Downloader class as well. This is a bad code practice as a good design is all about loose coupling.

Another problem with Inheritance is about finding where the exact code is. If the hierarchy has more levels, you are bound to encountering more issues as you will have to dig deeper into which class consists of the code you are looking for. Inheritance defines that the derived class is a type of the base class that follows “is-a relationship” just to reuse the code that exists in the base class and by inheriting mostly functions that are not needed.

Always try to go with composition which follows “has-a relationship.” i.e., a class can be composed of multiple classes. You can learn more about how to use of composition here in this article.

Vinay Ganesh

Written by

<Coder/><Guitarist/><SongWriter/>

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade