Inheritance & Polymorphism

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
now quite some time ago on this channel we did an episode on abstraction which is of course a programming concept that also applies in among other places to object-oriented programming or p4 shots this I guess is what's prompted YouTube user them enough to actually write the following comments which I thought yeah why have I not done an episode on this topic which is why today we're going to spend some time to take a look at inheritance and polymorphism one of the most powerful aspects of object-oriented programming you're watching another random Wednesday episode on zero six one two TV hello and welcome back to another random Wednesday episode today we're gonna talk about inheritance and polymorphism which are closely related to concepts of object-oriented programming which is why before we can look at these things let us have a quick refresh of what you know object-oriented programming or p4 shot actually is all of the examples you're gonna see today I'm gonna come to you in Java that's not to say other languages don't do all P equally well I just thought well Java is what I'm most familiar with so hopefully I can explain it as well as I can the key mechanic behind olp is the concept of classes it is essentially a data structure describing a type of objects when you're done describing a type of object you can then create actual instances of this object and have them basically you know interact in your program one cool thing about classes is that they hold both the data and the methods involved for you to actually manipulate objects of that class so right off the bat let us begin by actually creating a class we're gonna keep things simple here so let's just talk about a ball class which is why as you can see in our definition of a ball here we not only have information like its color its size we also have a function called kik which actually allows us to move the ball around now for the sake of simplicity these balls will be placed in just a simple one-dimensional well so you can imagine an infinitely long axis and the position is just one particular value on that axis we're trying to keep things very simple here so let us very quickly walk through the complete set class as you can see some fields are marked as private what private means is that well basically this is a value that cannot be touched from outside this of course is in contrast to something that is public so for example if I have this public function here what I can do then is I can instantiate a ball and then run the kick function from outside of the ball class I will not be able to access the size farival in a similar manner because that particular variable has been hidden away and we do that by setting it to a private variable we can also use protect it but at this point of time I'm not gonna explain to you what that means you will have a very good understanding of its meaning by the end of this episode also one of these functions is called a constructor this allows us to instantiate an object and what this means is simply in this case to create a new ball of course you have to be able to do that otherwise you know that class definition is kind of useless in our constructor we supply two pieces of information the color of the ball as well as its size so now that the class is ready let us write some code in our main function that will basically instantiate several of these ball objects and try to manipulate them as you can see the way we can manipulate this list of objects is extremely intuitive we simply create an array of the ball type immediately instantiate several of these objects and we're ready to go we can even use a for loop to loop through these objects and to actually manipulate them as you can see in our loop which goes through every single ball actually runs the kick function and then does a print this print of course maps to the two string function that we've written earlier all this does is it tells us information about that particular ball objects so let us quickly run this code and see what results we get as you can see everything works as expected all the information is stock per object and as you can see the kick function has advanced every ball three positions forward and that is your basic run-of-the-mill olp we've created a class with instantiates at several instances of this class and we've basically tested to see if they work so let's actually move on to the fun part which is the inheritance now we happen to know that not every type of ball should be kicked for example if you were to kick a bowling ball it will probably hurt your feet quite bad whereas if you take the ping-pong ball it'll just fly away let's try to capture the essence of that we could create a brand new say ping pong ball class but let's not do that let's base it off the class definition that we already have what I'm gonna do is I'm gonna create a ping pong ball class that inherits from the ball clubs what this means is basically I'm saying I want to reuse that class but I want to make some tweaks here and then anything I don't specify will be exactly the same s was defined in the ball class let's see how this realistically works when we create our ping pong ball class all we want to do is we want to have a new kick function the previous kick function simply advances the ball by three positions in this case let's make it at France by seven that essentially is the idea of inheritance and polymorphism we say we want to have everything the same except for these things which will then proceed to say how we want these things to be different in this particular case there are some cooks that you have to look out for for example in this case I'm not actually allowed to simply inherit the constructor I actually have to rewrite the constructor and not just that I have to express the constructor in terms of the ball constructor so like I said just a little quirk really all I'm doing here is I'm just passing all the same information to the super function and what that means is simply the constructor of the superclass against the terminology needs a bit of clarification yep a superclass is your parent class in other words it is the class you are inheriting from similarly a subclass is your child class which is the class that is inheriting from you so that is all well and good we now have a ping-pong ball class and a bowling ball class and they both inherit from the ball class what can we do with them and why is this different from just creating wholly new independent classes you see the magic once we move over to the main code all I'm gonna do is I'm just gonna change these two constructor calls the constructor calls of these new inherits at classes notice what I'm doing here I'm actually creating an object of the ping-pong ball class and I'm trying to stuff it into an array that was designed to hold objects of the ball class as you can see Java is in complaining that is in fact a legal thing to do that is in fact completely acceptable this is the power of inheritance you can actually treat a subclass as its parent class and you can interact with it thinking of it as its parent class because it has inherits at all these properties from its parent class you can imagine it to be like its parent class and interactive it the same way as you can see our court runs just fine and in fact the output shows that the polymorphism has taken place the ping-pong ball has traveled further when it's run the kick function and the bowling ball has travel less and all this has happened despite the fact that every object was treated as an object from the ball class that is the power of inheritance and polymorphism of course there are some limitations and drawbacks and you know problem areas that we have to look out for for example you remember earlier I mentioned that you know fields or functions can be or public you may have noticed that mysteriously some of the fields have turned in to protect it you see there is actually a difference let's say now I add a new private field to the original ball class notice that in its inherited class I cannot change or read that value at the same time I have no such problem with the protects at items this is actually deliberate this is actually something in a design the intent of having a protected item is from the outside it looks private that means I still cannot change the size of a ball from the main class however these subclasses are able to do so it appears public to the inherits at classes a private field will appear private even through the inherits and subclasses and that is a distinction that needs to be made in addition there is another limit let's say now we actually add a roll function to the bowling ball and of course the idea is you know we can roll it the thing is if we were to go back to our main program where our bowling ball was actually you know something in the ball array the roll function is not available to us the reason this is so is because we are still talking about this ball as a Bop not a bowling ball and there is a huge difference here because the roll function is not defined for objects of type ball so that is one pitfall you have to look out for as well functions and fields belonging to these subclasses cannot be accessed if you're looking at an object from the point of view of the superclass now we're basically done with this episode but I feel that this will be you know more rounded off if I could actually give you a few more Tippit's of information so let's begin first of all a superclass does not actually have to be fully implemented you can't actually come up with a class that is designed to be extended it is not meant to be used or instantiates it on its own it has to be extended by the programmer and then the programmer can use it next up we mentioned polymorphism earlier and while this term is used often in context of oval P it actually extends beyond that as well at its simplest polymorphism simply refers to overloading for example you can have two functions that have to save me but taking slightly different parameters these different inputs actually make them appear like different functions to the kampala and basically when you actually call this function the compiler chooses which version looks the best by looking at your input variables so actually this is very cool you can't overload a function as much as you need and that'll make things very convenient for the programmer because then they can put in whatever inputs they want and things will still work nicely and that basically wraps it up for this episode which basically raised through a number of concepts in a pretty short amount of time so we didn't manage to go in depth for a lot of them but hopefully this gives you a good general idea about all of inheritance anyway that's all there is for this episode thank you very much for watching and until next time you're watching 0 6 wants you TV thank you very much for watching if you like this video consider checking out the rest of my work on my channel alternatively you may be interested in a playlist of my earlier work on computing and computer science topics if you'd like to show me some monetary support I am on patreon you can find a link to my campaign in the video description of course you can simply like this video or leave a comment I'll be sure to respond as soon as I can to keep in touch with my future uploads do subscribe to this channel and for even more updates check out the official Twitter account for this channel @ 0 6 1 2 TV thank you for your support
Info
Channel: 0612 TV w/ NERDfirst
Views: 13,882
Rating: 4.9463806 out of 5
Keywords: Inheritance, Programming Language (Software Genre), Polymorphism (Field Of Study), programming, language, java, computing, computer science, computer, science, polymorphism
Id: QdHkMDVq8ms
Channel Id: undefined
Length: 13min 1sec (781 seconds)
Published: Tue Nov 03 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.