BrandGhost

Inheritance In OOP Explained - And Why You Shouldn't Abuse It

Are you a beginner programmer learning about Object Oriented Programming (OOP)? If so, you've likely heard of Inheritance, but what if I told you that I suggest you unlearn it for something better? Check out this video for an intro to inheritance and then see what's next! For more beginner programming videos, check this out: https://www.youtube.com/watch?v=_v9k74gWJtE&list=PLzATctVhnsgh_G9L3jgFROZKXmVLSUG8L Check out more Dev Leader content (including full in-depth articles with source code ex...
View Transcript
and yes I did mention this was going to divide people but it's all for the sake of being able to teach people about inheritance so that you can unlearn it after all of this I'm going to give you a crash course on inheritance and then ask you to unlearn everything I teach you about it so that you can learn the better way to write your software when people are learning about object oriented programming inheritance is one of the first things that we start looking at and it's ingrained in all of our heads as Junior software developers so before I dive into it by show of hands in the comments how many of you were told go focus on inheritance and that's the way of object oriented programming and I'm going to go ahead and spare you for my whiteboarding skills and my Ms pain skills because neither of those things you'll ever want to see let's head over to PowerPoint instead quick disclaimer it's going to be really bright because of the white background great so in PowerPoint here I'm going to be trying to explain visually how inheritance works I think what we're learning about object oriented programming the concept of inheritance is really helpful because there's a lot of parallels to things outside of programming that you can relate it to so as the name itself implies quite literally when we're talking about object oriented programming and inheritance the idea here is that we're going to have things that inherit other properties and characteristics from other things visually this is going to create a hierarchy so let's have a look at this hierarchy that I've drawn with these little circles in this hierarchy I have a yellow circle at the very top that will consider the parent or the ancestor that's going to be this one up here below the parent I have another yellow circle that will be the first child that inherits from the parent you'll notice that when we go one level down I actually have two children of the first child and they start to create a diverging path here so I just wanted to be able to show you that when we have inheritance we can actually have a branching structure in the hierarchy and you'll notice at this very lowest level of the hierarchy I have another green circle that inherits from the green circle above it so quickly to recap at the top we have the parent or the ancestor this green circle that I have highlighted here at the other end of the spectrum would be one of the lowest level children in this hierarchy you could also make the argument that this blue circle that I have highlighted here is also a child at one of the lowest levels of the hierarchy it's just that it's on a different branch it has fewer things that it's inherited from but for both this blue circle and this green circle at the bottom they don't have any other children below them so at this point you're probably saying that's cool Nick nice circles but I thought you promised us this is going to relate to parallel things that aren't in programming and circles just don't really cut it there are so many examples that we can pick from here but we're going to talk about animals today because animals are awesome so when we talk about the hierarchy and related to animals let's actually put the parent most ancestor right at the top and that's just going to be animal now there's a bunch of different ways that we could start to go build out this hierarchy and because I don't have Infinite Space and infinite time to show you we're just going to narrow it down a little bit and pick some concrete examples that we can play with so One path that we might go down with animals is actually that we're going to pick a subcategory of animals called mammals now before going any further in the hierarchy let's stop to pause why are we even breaking out animal from mammals and then continuing on this hierarchy to begin with well when we're talking about about inheritance what we're able to do is Define some type of properties or characteristics of something and then a child is able to inherit those things and then make adjustments to tune them more specific to the child one of the main reasons that we end up doing this in object-oriented programming is so that we can Define code in one spot and have other things reuse it so for example if we had code that defined characteristics and traits of an animal the idea of using inheritance is that if you wanted to have a more specific mammal class to deal with that instead of recoding all of the things that animal has because mammals also have those you would just be able to inherit from animal to gain all of that functionality without copying the code keep in mind that I want you to stay right to the end of this because I want you to unlearn everything I'm saying but this is really important as basic concepts in object-oriented programming now of course even with mammals there's still going to be so many different paths in the hierarchy that we could go build out but I'm gonna pit two groups of people against each other right here and I'm gonna pick cats and dogs alright so cats and dogs work in this example because both of these paths that we could take in the hierarchy they are both mammals so that will mean whatever we have defined for a cat and a dog can inherit from a mammal which also inherits from an animal all of the common pieces would not have to be redefined and we could reuse them so let's pick a property that we could go all the way from the top of the hierarchy down to cats and dogs and see how that might change as we go from each step in the hierarchy I'm going to pick a super basic one to start with and I'm just going to have a property that is the number of legs now if we have number of legs can I put that all the way at the animal category in this hierarchy so I want you to pause and think about that for a second think about all the different animal types you know and does every animal type have legs for those of you that are keen and know your animals the answer is no not every animal has legs however we could make an argument here that yes we could include the number of legs as a property but we don't have it actually defined to be a certain amount on animal so if you're thinking about a fish or a snake and they don't have legs yes you could have number of legs and set it to zero when we go to mammals though how many legs do mammals have well mammals have four legs so we could actually basically enforce that anything at the mammal level and Below has four legs and I'm going to interrupt myself here like an idiot because here I am trying to poke fun about people's knowledge about animals and then I'm going wait do all mammals actually have four legs and I need to clarify this I guess because I felt really stupid after pausing and then going hmm I actually don't know the answer to this but just a quick aside I guess we're looking at four Limbs and then another super quick note yes things like whales also don't actually have legs but that's because due to Evolution they've lost them so let's go correct this let's go put in number of Limbs and we'll change it in both spots here now the cool part about this is when we get to cats and dogs cats and dogs also have four limbs we can inherit this property from mammals and not have to make any alterations and for those of you with cats and dogs that don't have all four limbs I'm sorry I'm not trying to single those out I'm just trying to use this as an example that people might understand so that's interesting because we're able to at this point reuse the number of Limbs property from going from animal mammals to dogs or cats but how does it look if we start to go one level deeper so I've gone ahead and actually broken down our final tier of children into two more groups and this way it can better illustrate this example you'll see that I have keyshawns and devil squirrels now we can go introduce one more property to see how this fits into the hierarchy and that's going to be cuteness overload and yes I did mention this was going to divide people but it's all for the sake of being able to teach people about inheritance so that you can unlearn it after all of this now when we're looking at cuteness overload that's not really going to be a property of animal and it's not going to be a property of mammals either let's go down one more level in the hierarchy to cats and dogs now cats not cute not cute at all dogs on the other hand absolutely could have a cuteness overload property so we're gonna put that right here beside dogs if we go to the final level in our current hierarchy and we're looking at keyshawns versus devil squirrels we would say that absolutely yes keyshawns are cuteness overload and devil squirrels on the other hand are absolutely not cuteness overload so what is this example actually illustrating then well cuteness overload becomes a property that dogs are able to Define on them but then the child classes in this case the different breeds of dogs that I'm talking about are able to override whether or not they have a cuteness overload when we think about this from a programming perspective that would mean if you had a class that was animal you would not be able to access a cuteness overload property on it if you had a class that was a mammal you would also not be able to access a cuteness overload property on it even a cat that was a class that you had would not have a cuteness overload property on it but as soon as you had something that was a dog yes you would be able to access a cuteness overload property however you would need a specific implementation of a dog in the implementations that I have here in this example are keyshawns and double squirrels and each of those implementations would be responsible for providing a value for cuteness overload so yes these two classes would have a property called cuteness overload that you could access so in a nutshell that's how inheritance works and I purposefully went through that pretty quick so that we wouldn't spend too much time hyper focused on inheritance inheritance is one of the first things that we're all taught when we're learning object-oriented programming but I have a strong opinion that we end up hyper focusing on it and that we don't end up branching out into some of the other things that can do a better job of inheritance the result of this is that as more Junior programmers gain experience and keep writing software because they're so familiar with inheritance they end up writing a ton of code that has really large inheritance hierarchy and as you become more experienced as a software developer you'll find situations where these really large inheritance hierarchies can actually really couple your code together make it hard to test make it inflexible make it hard to refactor and in general just a pain in the butt to work with so if you like my photoshopping skills please give this video a thumbs up subscribe to the channel and as promised because you watched all the way right to the end you're going to want to watch this video right here so that you can learn more about the other thing that's way better than inheritance that you really need to focus on

Frequently Asked Questions

What is inheritance in object-oriented programming?

Inheritance in object-oriented programming is a concept where a class can inherit properties and methods from another class, forming a hierarchy. For example, if we have a parent class called 'Animal', a child class 'Mammal' can inherit characteristics from 'Animal', allowing us to reuse code without redefining it.

Why is it important to understand inheritance before unlearning it?

Understanding inheritance is crucial because it lays the foundation for object-oriented programming. It helps you grasp how classes interact and how properties can be reused. However, I encourage you to unlearn it later because there are often better ways to structure your code that avoid the pitfalls of deep inheritance hierarchies.

What are the potential downsides of using inheritance?

The potential downsides of using inheritance include creating large and complex inheritance hierarchies that can couple your code together, making it inflexible and hard to test or refactor. As you gain experience, you'll find that these hierarchies can become a pain to work with, which is why I advocate for exploring alternatives.

These FAQs were generated by AI from the video transcript.
An error has occurred. This application may no longer respond until reloaded. Reload