Inheritance
In this video we compare between cats and dogs and realize that there is a lot in common with them. as we explore this we try to figure out a way to share functionalists between them as they are both "pets" or "mammals". With this idea we share our next oop topic of inheritance.
Video Transcript
00:00So, our next topic is inheritance, and that's exactly what you were starting to talk about.
00:05And for that example, I just put you on the stage two theoretical classes.
00:09One class that creates cats, one class that creates dogs.
00:13Now, automatically, you can see that there's a lot of resemblance between cats and dogs.
00:20They both eat, they both drink, they both poop, they sleep, they walk on four, they
00:24wag their tail, and a lot of other stuff that they do similarly.
00:28Cats scratch stuff while dogs sniff stuff.
00:31A little bit quirky differences between cats and dogs, although there's other stuff.
00:35But, in front of the hood, the public stuff are basically the same thing.
00:40Although a dog will eat different food, his mechanisms are the exact same thing as a cat
00:45for a programming language.
00:47A cat and a dog are the exact same thing for a programming language, because it's all
00:51virtual, right?
00:52So, in our scenario, a cat and a dog are really almost the same thing, limited to
00:56a few minor changes, where a dog will make a different sound, and a dog will drink more
01:01maybe, and a cat will drink less, and a dog will sleep less, and a cat will sleep more.
01:05But these seem like really big commonalities.
01:10So if we take this into account, and we say, okay, there's a lot of things here that
01:13are almost the same thing, it doesn't make any sense for us to go and recreate these
01:18functions over and over and over, because now they're only dogs and cats, but imagine
01:23that we're creating a zoo.
01:25And every mammal eats, every mammal drinks, every mammal poops, every mammal sleeps.
01:32So if we wanted to, we could have created a mammal class and shared that throughout all
01:36our different elements.
01:38Now, one thing that I want to encourage you and recommend you doing is don't over plan
01:43when you just start working with object oriented programming.
01:46You over plan, basically, will just get so complicated that you'll get to a place
01:51where you'll just hate object oriented programming.
01:54Instead of that, build the wrong way, but don't continue building the wrong way.
02:00Build the two classes, build a dog class with all the stuff, and the cat with all
02:04the stuff at the beginning, until they start getting familiar.
02:06And then when you build it, and you look at the two of them and say, whoa, whoa,
02:10whoa, these look really, really similar.
02:12Is there any way for me to maybe change my code before my code gets really,
02:16really big and unusable?
02:18By the way, this approach of coding is called agile development, more or less.
02:23As you get more comfortable, there's stuff that you will just do automatically.
02:26But the idea is basically, instead of over planning, build your classes or
02:30plan your classes and then see, okay, will these really share a lot of commonalities?
02:34Well, their commonalities are more than just mammals,
02:36because they're so related to each other, maybe they're going to be house pets.
02:40Or something that is closer, because who cares about other mammals?
02:45We're not building a class that will support whales.
02:48We're building an application that's only about cats and dogs.
02:51So don't over plan, and a lot of people over plan and plan for stuff that they'll
02:55actually never build, and then their applications get really complicated.
02:59So, but back to our topic, if we condense that list, we'll see that all these
03:04stuff is shared, all these stuff, both animals use, and these stuff are really not.
03:09So then if we look at it in this way, we could say, how about we create
03:13another class, and by the way, this is the typo, this is not supposed to be cat.
03:16And have those two inherit the properties that are and
03:21functions that are created here, and share them with each other.
03:25And that's really the whole point of inheritance.
03:28And in object oriented programming, and especially in Flash,
03:31everything really is inherited.
03:33And actually, the only thing that doesn't inherit from something else
03:36is the object class.
03:37And the reason is, is really because every single thing needs to have some
03:42sort of common, it has some sort of common commonalities.
03:46The more detailed an animal is, the less important the commonalities are for
03:50you, right?
03:51So if you look at it as a human being, and you look at a human scenario, or
03:54let's look at a cat scenario, to create a hair particle or
03:58a fur particle at a cat is a very complicated thing.
04:02Now, after one fur particle was created,
04:06I don't actually need to know how that particle was created anymore.
04:10That could have been a class, a hair class, a fur class.
04:17The fur then created the whole body, and
04:19that fur could be used on any animal at all, right?
04:21It could be, every animal has hair or
04:24fur, almost any animal, unless you're, well, any mammal of sorts.
04:28So let's say every mammal would have these specifications of hair, and
04:31growth, and skin, and all these stuff that just every single animal has.
04:36And in the exact same way in code in a computer,
04:40there's just some stuff that every single object needs.
04:42If it's had to print itself onto a trace when you're tracing something out.
04:47If you're a visual element, the capability to add you to the stage.
04:50If you're an interactive element, the capability for
04:53mouses to click on you.
04:55If you're elements that users could interact with, you have also events and
04:59all these different type of things that every single visual element would need.
05:03So in our scenario, we're building this class that's basically enabling us to not
05:08know more than that after we create this mammal class, let's call it,
05:13even though it's, you know, ignore that cat, and let's call it mammal class.
05:16And after we have a class that enables things to eat, drink, poop,
05:21sleep, wag their tail, and so on and so forth.
05:24We don't need to know how eating works.
05:26We don't need to know how drinking works.
05:28Not only don't we need to know it for other external classes, but
05:32now when we're creating our cat, the cat doesn't need to know how they work.
05:36The cat just needs to know what works.
05:38It just needs that public function.
05:40So if my cat needs to internally call it eat or whatever, that's all I really need
05:44to know. And for me to add another scratch stuff, it has really nothing to
05:48do with all the rest, so done.
05:51And I just added a feature.
05:52And when it goes down to the code itself and you're looking at an application.
05:56And I do wanna show this just for a second,
05:58even though this is not our main topic right now.
06:00But if we go into our, just create a quick, quick little, oops.
06:05If we create a quick little script, and I'm gonna create our new class.
06:10And I'm just gonna give it a name back.
06:13Well, let's lowercase all the rest.
06:16All right, so we created a class called back.
06:19All it really is is you call an extends, and then you say what it extends.
06:23So everything extends an object, but if we created something that's more
06:27sophisticated, such as we already created the mammal class.
06:34So let's say, mam class, and we have your rcat class, or rcat extends mam.
06:39Now, when rcat extends mams, it doesn't need to know at all how it works,
06:42unless you need some specific changes related to cat that are in the mammal,
06:46and you'd wanna change and send.
06:48But that's already technicality, so you'll figure out as you go.
06:51And that's really the whole point of inheritance.
06:54Inheritance really helps you to hide all that private information that you
06:58don't really care about anymore, cuz it will work for every single mammal.
07:02And you wanna add new stuff.
07:03Now, once in a while, you're gonna have to figure out a way how to override stuff.
07:06And once in a while, you're gonna have to do some minor changes and tweaks and
07:09edits and whatever.
07:10And you'll figure them out as you go, and you'll learn them on our site, or
07:14you'll learn them in another place.
07:15But the whole idea and the whole goal of a class is to really hide from you
07:19things.
07:19The more things you hide from yourself, the better of a developer you'll become.
07:23Because the less things you need to know about.
07:26The more things you know about, the worse you are as a developer,
07:29not because you're a bad person, but because you need to know about more things.
07:33Your success as an object-oriented developer is really your success in
07:37hiding things from yourself that you don't need to know about.
07:41And don't try to be, especially if you're new to object-oriented programming.
07:45The goal is not to learn every single terminology as quickly as possible.
07:50The goal is really for you to build applications that are easy for
07:55you to build.
07:56And as you learn different features and as you add more things into your
07:59arsenal, which we're gonna talk about a couple of them now, their goal is not
08:03to complicate your life.
08:05So if you add something in after these basic concepts and it complicates your
08:09life, then scale back out of it and ease yourself into it slowly.
08:13Because it's really about how you master the hiding of information
08:17that will make you a master developer in object-oriented programming.
08:21The more you hide, the more things you can do.
08:24And I just want to say just one more thing about this.
08:26We all have, and I wish my camera was on, we all have ten fingers.
08:32There are ten digits that are really standard.
08:35We usually think in numbers from one to ten.
08:38Our brains are really collaborated throughout history to think in groups of
08:43up to ten.
08:44Now as you become a smart and successful developer, and hopefully,
08:50well obviously you are already, and hopefully you'll become even better ones.
08:53Your biggest goal really is to not have more than ten things that you need to
08:57think about at any given time.
09:00Even less than that is better.
09:01The less things you're thinking about, the more things you create.
09:04Which sounds weird, but basically the less you're exposing yourself to things,
09:10the more complicated and amazing things you'll create.
09:14And this is a thumb rule that I have that I sometimes don't follow, but
09:17I try to follow ten lines of code inside of every function,
09:23ten public functions in every class, no more than twenty or
09:26thirty functions in total inside of a class.
09:30But my goal is to have ten public functions, not more than that.
09:33And if I need more than that, then I create more classes usually.
09:37Whenever you have too much functionality in one specific thing,
09:39usually it means that you gave one class too much responsibility.
09:43Okay, so let's continue on in our presentation a little bit,
09:50and then I'll loop back in for questions.
09:52I don't even remember what's the next thing, so, encapsulation.
09:56All right, so this is a boom, a big, big topic.
09:58Not that big, more, cuz it's just a scary, big word.
10:01And really our goal here is to kind of break down some of these scary words and
10:05kind of get you more familiar with them.
10:06So before I get into encapsulation, I just wanna make sure that everyone's
10:09still with me, am I going too fast, is everything okay?
10:14Yeah, yeah, you could go crazy with traces, traces non-stop.
10:20All right, everyone's still with me.
10:21Do you guys want me to go faster, slower, any questions?
10:24Do you want me to go back on something, or
10:26are you ready to encapsulate our next topic?
10:30Sweet, all right, good stuff.
10:33Excellent, thank you guys.
