4.5: Introduction to Inheritance Part I - The Nature of Code

4.5: Introduction to Inheritance Part I – The Nature of Code

Views:15613|Rating:4.93|View Time:13:7Minutes|Likes:341|Dislikes:5
This video covers the topic of inheritance as it relates to object-oriented programming in the Java programming language.

Read along:

Help us caption & translate this video!

📄 Code of Conduct:

if you've been watching these videos you've been you you're you are aware of object-oriented programming everything we've been doing has really been kind of built on top of object-oriented programming we write classes that are templates to make these objects that move around the screen the objects have data and functionality that manage their physics and how they're drawn on the screen and we've been doing this over and over again this is and that the principle we've been kind of living by is called encapsulation we are encapsulating all the data and functionality into this class and we're making objects from it this has been great I hope you've enjoyed it I've been enjoying it but there is a missing piece here there is another part of object-oriented programming that we haven't explored and I think it's worth exploring in this thing that we're doing whatever it may be and in making these videos or watching these videos or whatever it is we're doing and that is this idea of inheritance and polymorphism so so let's think about this for a second okay so right behind me over here we have this Vanna White over here we have this particle system right we have this particle system and it has this kind of generic particle we've made this generic particle class I could kind of click over here we could see it click to come on and we could see sorry we have this generic particle class edge location velocity acceleration it's drawn as a circle what if however we wanted to make this particle system full of all different kinds of particles star particles and rainbow particles and twirly particles and blue particles that's they're all they look different they act different but they're all basically the same they're all in the same system they all have a location velocity and acceleration they all fall with gravity how could we do this well there's a bunch of different ways on the one hand we could just start adding lots more variables here and kind of have boolean x' like to keep track of which kind of particle should be drawn this way and do this thing on the other hand we could say let's make some more classes let's for example make a star particle class and and if that was our instinct we would go over to our program and we would say mm hmm hello program and we would go over here and we'd say new tab and we'd make type our new tab and we call it star particle ah star particle tab and we say ah my star particle it's really basically the same thing is my particle so let me go over here to my particle tab and I'm just gonna select all and hit copy and then I'm gonna go over here to star particle and I'm gonna hit paste then I'm gonna rename this star particle and type star particle here then I'm gonna start modifying this code so let me just say something here anytime that you copy/paste huge amounts of code from one section area gram to another you should be suspicious and you should ask yourself if there if could there be a better way is there another organ is there somebody else that's somebody else you can counter this situation before and is there a better way that I could organize my code and the answer to this is yes in this scenario that we are talking ourselves through we're gonna make a new kind of particle that's essentially the same with some slight modifications this is where inheritance comes in we want to say that our star particle inherits everything that a particle ever wanted to be in its life with a few slight changes that we're gonna make to it and let's look at so so before we can go and add this to this particular program let's kind of map this out how it works over here over over there I'll be right back okay we're back over here now okay so let's forget about the particle system for a second and let's just think about the animal kingdom and let's say we are writing a class called mammal a class mammal curly bracket close curly bracket now mammals they have generally have fur they give live birth to their young again I have no actual knowledge of paleontology or science just you know things that I read in children's books but but but we could start making something up we could say okay well this is a program we're simulating a mammal and one of the things we're gonna have is a color which will be its fur color and then another thing we might be able use a boolean to describe its gender you know true for female false for male something like that and then we're going to write some functions you know what do mammals do we need to have a function that we can call when it's time for the mammal to go to sleep it's gonna have some code in there and we need to have a function when it's time for the mammals to eat and we'll put some code in there so you know we've writing this class and we do this all the time with our mover or particle what but let's say now we're so we have this generic mammal and now it's time piqué a mammal out of a hat cat I can't think of anything better the internet loves cats so we're gonna maybe we should make our classic kitten so I'm just big our classic kitten it's a little cuter so we're gonna write a kitten class a class called kitten and a kitten is also gonna have a fur color and a kitten is also gonna have a gender and the kittens gonna eat and a kitten is gonna sleep and maybe a kittens gonna you know purr and meow and have other things that it does too so how do i how do we deal with this we could copy-paste all this code so we start from the base mammal class or we could use the principle of inheritance and the way that we're going to use the principle of inheritance is with some new code that you well if you're watching this video hopefully you haven't seen it before if you have seen it before turn it off and go you know watch Star Wars or something okay class kitten extends mammal this is pretty important I hope it's not cut off extends mammal this is the new syntax for inheritance what we're saying is kitten is a child class of mammal mammal is a parent class of kitten we also used the words superclass subclass but but extends is the keyword we write in our code to indicate that kitten extends mammal and what this means is that kitten inherits everything from mammal it's as if we wrote all the variables from mammal over here we don't have to type them or write them in they are assumed now it's as if we wrote the identical sleep function it's as if we wrote the identical eat function over here so these are all the things that happen with inheritance automatically if boom we're done we have basically a kitten class is done so we have to ask yourself why we do this in the first place we're doing this not because we want kitten to be an exact replica of a mammal we want kitten to inherit everything that a mammal has but also have some of its own things so for example we might add so I want to make a list really wish this whiteboard was bigger so someday we're gonna have some kind of sliding door whiteboard system but um what I'm gonna do is I'm gonna just carve out a little space over here in the corner and I'm gonna make a list of the things that you can do with inheritance so number one is you can you inherit everything so we now we see that so a kitten has fur color agender it has a function called sleep as a function called eat if we remember that's what these functions were called sleep and eat okay what's another thing that we can do we can add data or functionality right so we can add data or functionality the kitten might have a integer which is the number of whiskers for example so this is not something that all mammals have I don't think I'm a mammal right I don't have whiskers not really anyway so but kittens have whiskers so we can add variables that are only for the kitten we can also add functions so maybe we might add a function called a meow and meow is something that only kittens do not all mammals do so we repeat function we're going to add to the kitten class great so we could one of the things we can do with inheritance is we can inherit everything and then add to it the other thing we can do which will use the term is we can override functions what do we mean by override functions well it might be that okay so we our mammal class has a function called sleep all mammal sleep cats and dogs and porcupines and I don't know lots of other interesting mammals that I wish I could think of right they all sleep so does a cat sleep yes a cat does the kitten sleeps but what if a kitten sleeps in its own special way that's different than from how all that sort of it's a parent class sleep so we can override a function if I write the function sleep here what this does is it says well instead of when it when it when I call kitten dot sleep instead of doing the the functionality that was inherited execute the functionality that's now in my child class that's now in my kitten class so you can override you can inherit functions but you can always rewrite it in the child class to override that functionality so that's one of the other things we can do with inheritance the fourth thing which I'm sort of making up I guess I think I might be making all of this up because I just live in a crazy world with green screens and lights and whiteboards by myself but this this thing that you can do I'm gonna call is you can kind of inherit you could just I don't know what to call this you can you can override plus and Herod you can modify you can kind of do both you can you can you can I'm just going to write the word super this this fourth thing you can do is really super what it is boy do i botch this is that you could say if you wanted to I want to modify the sleep function but also call what the what the what the what happens in the parents leave function ah I can fix this problem we've encountered let's be a little more specific I am gonna write the mammal sleep class and the mammal sleep class sorry the man will sleep function just has a print statement in it which says right you print a bunch of ZZZ that's what it means for a mammal to sleep you bunch of Z's get printed out of the screen now I'm gonna override it and I'm gonna say sleep no no a kitten does not when it sleeps what a kitten does is it says purr okay so this is overriding mammals kittens / what I'm really turning into a crazy person so this is number three overriding but if we want we could both inherit and override so we could sort of inherit the function but add something to it and the way that we do that is by saying super open cases right dot sleep so what super dot sleep does is it allows you to call what it does is it calls the functionality that's in the superclass the parent class so now for a kitten to sleep a kitten goes / / so without super dot sleep it's just / without putting this in at all it's just we inherit this this is now we can do both if we wanted to we can always call the parent sleep function as well by using the keyword super okay so uh I realized that this is a lot of information here and you're probably going to need to practice this and you're gonna need a scenario for which it makes sense to do this and I who promised myself yesterday that I was gonna make these videos shorter and this one is already about 12 minutes long but eventually the video okay I had a little mishap but I just want to wrap up this video so what we've done in this video here is we've kind of mapped out what it means to do to have inheritance so in the next video what we're actually gonna go do is start we're gonna we're going to look at inheriting the particle class and making a new particle object that inherits everything but adds a little bit of functionality to it so we're gonna see how to do that in the next video what I would suggest to you as an exercise before you watch it is try to do that yourself could you actually make a star particle which is everything that a particle was but draws itself as a star maybe the star spins or something like that see if you can do that yourself and in the next video we're gonna we will essentially that's exactly what we're going to do so that's it for this show

21 Replies to “4.5: Introduction to Inheritance Part I – The Nature of Code”

  1. Amar

    I have seen so many videos on OOP on YouTube but this was the most informative one good job and I had also seen the MIT videos as well

  2. Xy

    I used to program in Java and since i found out about this super awesome guy doing such good videos about p5 Js, processing and so on i simply had to start with js. Im glad i found this video on Inheritance because i juts spend the last half hour trying to find good explanation and useful tuts about this topic. Thank you so much for your great videos, its a shame you dont get much more attention. Even non programmes can and should watch you having fun and being yourself. We love you <3

  3. Alexander Kraus

    Wait… Did you write Mammal with an E?

    I'm still going to tell my programming teacher to check you out and stop teaching Java and instead start with Javascript/Processing. He's mainly a science teacher (which makes NOC even more interesting) and admitted that he uses the 5mins before the lesson to learn what he wants to teach us…

  4. Roger Causto

    Even if I do all your exercices in javascript, at this point, and I will continue go through your video and book, I learn so much from you. Thanks you so much. Big up and peace

  5. Robert Simmons

    These videos should be the No.1 watched videos on youtube for Processing. Cleared up so much stuff for me, great job!

Leave a Reply

Your email address will not be published. Required fields are marked *