C# Tutorial 7 Inheritance Polymorphism

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello internet and welcome to part 7 of my C sharp video tutorial in this part of the tutorial we're going to cover protected inheritance delegates superclasses polymorphism virtual override inter classes and a whole bunch more like always all the code as well as a transcript of this video is available in the description underneath this video and I have a lot to do so let's get into it okay so here I am in Visual Studio if you are on a Mac you can use the MER in exactly the same way everything is exactly the same alright so what we're going to do here is we're going to get more deep very deep into how object-oriented programming works inside of c-sharp first thing I want to cover is we're going to talk about inheritance but let's say that we do not want a subclass to be able to inherit from our class how do we do that well we just type in sealed and that will keep it from inheriting and by inheritance I mean that a class can come along and inherit all of the fields and methods from our animal class in this situation so let's come in here and start adding some things so I'm going to very first off I'm going to go private string and name I'm going to give a name for my animal and then I'm going to go protected string and sounds and by marking this as protected that means that this field data can be changed by a subclass directly and I'm going to show you exactly how that would work in an example here as the tutorial continues to just remember that sound is protected while name is private I'm then going to come in and I'm going to define a method and later on and make this virtual but right now I'm going to make it void don't worry if you don't know what virtual means by the end of this tutorial you shall so let's just come in here and say make sound and what I want this to do is to output some information on the screen in regards to the name and the sounds this animal makes and in this situation I want to use interpolation with the string that's going to be output you're going to see what that means so I'm going to be able to go and put a dollar sign here and then instead of coming in and going 0 like I did previously I can actually come in and instead type in name and then commit and say says and I can type in sound and I'm going to create those properties here in a second so you don't have to use the zeros in the ones as long as you put the dollar sign right there and sometimes that's convenient and sometimes it's not convenient now let's come in here and we will create our constructor and then we'll create our name and sound properties as well so we'll go in and say public animal and we want this to call a constructor that is going to receive more than one parameter so we're going to go this and then I'm going to say that my default is going to be no name as well as no sound and we covered that in the last part of the tutorial and then we just put our little curly brackets there and everything's great and this is just giving me an error because that hasn't been created yet so let's go in continue creating constructors we can also come in and go public animal and handle a situation in which only one parameter is passed in so here we'll say string and name once again we're going to come in and say this and here we will pass the name and then we will also pass no sound to the other constructor close it with the curly brackets and now we can create the constructor that is going to accept both of us so we'll say animal once again and we'll say string and name and then string and sound right like that and now this is going to be able to properly assign the and once again I'm going to call the property that hasn't been created yet let's see up it's not let me do it so what I'm going to have to do is come down here and actually create these properties so I'm going to go in and I'm going to say public string and name then inside of it I'm going to do a getter and it is just going to return whatever the value for name is set s and then I can also come in and do a setter for this and this is going to be more specific what we're going to say here again is that we're not going to accept something if the name contains a digits we have something against numbers as names for our animals we'll go any and this is going to check every single value inside of this string to see if it contains a digit we'll be doing some more cool things with things like any here very soon make sure that we come in here and go any and then surround that like that and in the situation in which we do not contain a or which we do contain a digits we're going to say that we want to just give it the name of no name and otherwise we can come out of here and say name is going to be equal to value remember whenever we are dealing with setters that are going to be auto-generated like this with properties that the value that is assigned and with the setter to name is going to be given the default name of values so you always have to use value in that situation now we're basically going to do the same type of stuff here with our sound properties so let's just come in here and do this and let's change this to sounds here we'll say return sound and just because sound is protected we do not need to come in here and change anything with it or do anything different with it and here we're going to say if and get rid of that let's say we want to keep the maximum size to be less than 10 so we'll say value and length and greater than 10 in that situation we're just going to assign no sound to it and otherwise make sure we have is that the sound of course and then otherwise we're going to go and assign the value that was passed in to sounds okay and now that we have that set up our constructor up here is going to allow us to type in names so we'll go in here and we'll type in name like that and we'll assign it to whatever the name was passed in and we'll also be able to come in to sounds in us on the sound value that was passed in now we have our animal class defined and now I want to go in here and start working with our dog class and you can see right here if you want to inherit all of the properties as well as methods that are assigned to animal all you have to do is put a colon and animal pretty simple stuff it's very important to be aware that you can not inherit from multiple classes I'll get into ways of getting around that later on but in this way right here you couldn't do something like animal and person if you wanted to do a man-dog or something like that okay so we'll get into that later on now it's very important whenever you commit here not only can you go and inherit all the properties and methods from animal you're also going to be able to add additional properties or fields so let's say that we want our dogs to have the ability to have multiple sounds we can come in and go public string and we're going to go sound to and to keep this simple I'm just going to go and have it generate the getters and setters and then I'm also going to come in and define a default if they decide that they do not want to pass any values inside of there okay so pretty cool stuff the thing that's important is we're going to be able to overwrite methods and I'm going to show you two ways of doing this one way is to do new and then type in void and then make sounds and that's one way to overwrite it I'm going to show you another way here in a moment like I said and in this situation it's a console and right line and then I'm going to use string interpolation once again and I'm going to do basically the same thing I'm going to type in name like that and as you can see it's not giving me an error because it's getting the name from the animal class we created earlier and I'm going to say says and there we go sounds and then I'm also going to go and output sounds - which is the other property that we use is just defined inside of there alright so you have that all set up so the thing that's important is that we will be able to define constructors inside of our new ant dog class and at the same time call our animal class to initialize values inside of it so how we would do that is just go public and dog and say that we want to receive a string name equal to let's go and give it a default value of no name and then inside of it will go string and sound equal to no sounds or actually let's go and do this inside of the other guy inside of the main definitions let's cut that out of there right there come down here paste that in there there's no sound and then let's also come in and do the same thing with sound too so those and we'll call this sound to is equal to no sound - alright so if you want to call the superclass or the animal class in this situation the class we inherited from and have it go and initialize everything for us what we're going to do here is we're going to get rid of that first off come down inside here and then we were going to say base and TAS in name and sounds remember it can't accept sound - so we're not going to pass that and then after that inside of our curly brackets we can then assigned the value for sound so so sound - like that so we use base to call the super class animal constructor to have it handle the values for name and sound and then we handle whatever is different about this class inside of that constructor now that we have both of those set up let's jump over into program dot dot CS and start playing around with these new objects we created alright so here we are inside of main and I want to go and create an animal type and I'm going to call it whiskers is equal to new animal and then I can define the properties inside of it so I'm going to say name is equal to whiskers and of course put quotes around this because it is a string and then after that I can also come in and define my sounds equal to meow and then make sure you put your little semicolon right there after that likewise I'm going to be able to come in here and let's do the same thing let's go and create a different one here I'm going to type in dog however instead of animal and if you need more time with a slide to type stuff in just hit the pause button I like to use the pause button here it gets lonely on YouTube and then I can go a new dog like that and then I can go name and let's have the name of course be Grover and then let's have the sound be wolf and then remember I also have a sound too inside of my dog class and let's just have that be ger just like that okay so pretty easy stuff so now let's test out and see what happens here in this situation I also want to cover how we can change remember we've came down inside of animal we have this marked as protected I want to show you how we can go and change that so must say Grover and then sound and then let's change this to wolf like that bigjon wolf and you're going to say it didn't throw an error because it's marked as protected and you were able to go in and change protected variables directly and then I'm going to come in and call for the make sounds methods and we can see how they differ between the two of these there's Grover and Grover's make sounds and we can run it and as you can see whisker says meow Grover says wolf and grrrrrr and you can also note here that the wolf changed because I was able to go in there and directly change it because it was marked as protected all right now something else let's jump over into animal something else that's important to understand is inheritance has a is a relationship so what I mean by is a is a dog is a animal Wow what is called aggregation or a delegate represents a has a relationship so let's say that we would like to have some information create another class that is going to define an object that is going to be stored in our animal class that is a has a dog or an animal has identification information so let's actually jump into animal ID info dot CS that's what it's called and you can see it's right there as well and we'll come in here and just define a couple different things so I'm going to say there's an int that represents an ID number and we're going to auto-generate the getters and the setters for that and give it a default value of zero and also what we're going to do inside of here is create a string that is going to represent an owner for the animal if we have one and that's going to be a getter and setter as well and the default value is going to be no owner all right so there we are and there we are so there we went and created animal ID info and now what we're going to do is show how is it how we can have a has a representation inside of a class so jump back over into animal I'm going to jump in right here after I define those two fields and I'm going to say protected animal I the info you can see it popped up right inside of there and then I can go animal ID info once again is equal to no animal ID info and it doesn't receive anything it just uses a default constructor so that's all great now what I'm going to be able to do is come in and say public void set animal ID info and have it receive an identification number as well as a strength that is going to represent the owner for this animal and want to pay reference to it and you just go animal ID info like that and ID number like this and pass in the new identification number for this guy and we'll just copy this as well and then we'll also be able to come in here and define the owner for our animal and I'm not doing any checks here but of course you could and you probably should and make sure that this is uppercase okay so we have that set we can also then come in and create a method that is going to pull that information out of there so we can say public and void and get animal ID info and doesn't receive any parameters and then what it is going to do is just output on our screen for us the information that we have in regards to that so we're going to come in and we're going to say name pay reference or our animal that we have here has the ID o and then if we want to reference that animal ID info guy we can just do that right there and then type in ID number and is owned by once again come in animal ID info and in this situation go and get the owner of set animal print all that information out on our screen and everything a little peachy jump back in program dot C s and let's test this out just to show that indeed we have set that up properly so here we're going to say whiskers and set animal ID info like that and it will pass in a number let's say one two three four five and then we'll pass in an owner and say Sally Smith owns that animal and then show also we can do the same thing for our dog because it automatically inherited all that so set animal ID info once again I'm gonna sit one two three four six and then we're going to say Paul Brown owns Grover with that inside of there and then we would be able to come in here and for both of these guys just go whiskers for example and get and yd info like that and then do exactly the same thing for Grover so like this and get-and like that and like that and if we run it you're going to see that whiskers had the ID one two three four five and is owned by Sally Smith and Grover had the idea one two three four six and is owned by Paul Brown okay so that's how we can use a delegate or an aggregate or whatever you want to call them they're basically just objects stored inside of an object and if you forgot what I was doing here we come back here and you can see I defined this animal ID info which is a reference to this class right here and it has all that information and it stores it inside of the animal class alright so pretty cool stuff and very useful stuff as you are going to see as we do bigger and more complex projects now I want to talk about the concept of inter classes now you can actually create classes inside classes or inner classes and they are normally used as helper classes for the outer class because the inner class will be able to access private members of the outer class that surrounds it so let's say that I would want to have an inner class in this situation and what it's going to do is provide me health information on my animals so what I'm going to do first I'm going to make sure that I am outside of any functions right here but still inside of the class which I am and I'm going to go just like anything else I can go public and class and what I'm going to do is call this animal health and then inside of it I am going to create a method that is going to return a true or false and it's basically just going to tell me if my animal is a healthy weight or not so what it's going to reduce is going to receive a double that's going to represent the animals heights in inches as well as a double that it represents the animals height in weight and I'm just doing something silly here I don't even know that this is true about animals just something I'm playing with so what I'm going to do is I'm going to say if the height is divided by the weight we're going to deem that this animal is healthy if a couple situations occur here if the value is greater than or equal to 0.18 and the calculation is less than or equal to 0.2 7 so I'm just making a justification based off of height what the weight of the animal should be like I said I don't know if it's is true actually I would guess that it's probably not true all right so those don't hold me by this I'm guessing this is just something I made up so in that situation I'm going to say return true and otherwise if that came back as false I am going to say else return false okay so there we are that's a demonstration of how we can have an inner class and a little helper function inside of it and add the functionality of this class now we can jump back into the programs ES file and we can start messing around with this so let's come down here right after this and let's work with that inner class so if I want to access the inner class what I'm going to do here is just go animal and then animal health reference that you by referencing the outer class that surrounds it I'm going to call this get health is equal to new once again animal dot and then animal health once again it doesn't receive any parameters and then from doing that what I'm going to be able to do is let's go do a console.writeline and I'll say is my animal healthy and we will find out if the animal is healthy or not then we'll go get help and then we can go healthy weight and then pass in 11 inches and 46 pounds and we'll find out if our animal indeed is healthy or not so let's run that is my animal healthy it comes back it's true and if we come in here and let's say my animal is 11 inches in 100 46 pounds a little bit heavy you can see that it comes back as false okay so there we are so there's a demonstration of inner classes and like I said as the tutorial continues I'm going to be doing real-world things with these things I just wanted to demonstrate how they work so that whenever you see them in a big project you're not like what is that an intra class okay so now you know what an inner class is and now what I want to do is talk about something called polymorphism now you're going to be able to come in here and you're going to be able to define two animal objects but have one actually be a dog type so let's say that you had like an array of animals you could actually store the different types even if it's a dog it's a subclass of animal as an animal but I'm going to show you how that can sometimes cause problems and then I'm going to show you how to solve that so I'm going to say new animal monkey doesn't receive any parameters and then inside of it I'm going to say name is equal to happy and then I'm going to say that the sound that our monkey is going to make is e e you know like that alright so there we go in we created an animal monkey object now what we want to do is we want to create an animal object that is actually a dog and so I'm going to call it an animal right here I'm going to give it the name of spot however we are actually going to call the dog constructor to create it and what that means is well let's come in here change this to spot let's change the sounds to wolf like that and then our dogs are going to receive another sound sound too and what we're going to do is we are going to find that as well now you're going to see what's wrong with us okay so they're both animals however defined as animals this uses an animal constructor this uses a dog constructor this is not going to throw an error but something else weird is going to have a problem is that if you call a function in animal it's not by default going to call the overridden method in dog unless that method is marked as potentially over writable by marking it with the keyword of virtual so what I'm going to do here first is I'm going to show you what happens if we try to call make sound on both of these so we'll just say monkey thefts and remember we have the make sounds method inside of there for the the animal class I'm going to call spot and make sounds once again for it and we're going to see what happens all right and you're going to see happy says E and spot says woof okay but where's the second sound well if we jump over inside off dogs jump into dog here done that and let's look for make sound you can see it's supposed to print out both of the sounds it doesn't however and what we need to do is we need to mark it so that it knows that it should allow for it to be overridden even if this animal object is referenced as an animal instead of as a dog this is polymorphism this is the ability to go and label a class as its superclass name however I also have it act as the subclass itself dog and whenever it is actually a dog to call the right method to call this method make sound instead of calling the animal version of make sound which is where is it make sound right there so we don't want to call this one we want to call the other one how can we fix this actually pretty simple what we need to do is come in here and say virtual what that's going to say is that the make sound method inside of subclasses may override this and in the situation in which it does override them use those methods instead and then we go into dog and how do we make this make sense instead of new like we have right there we type in override and we do that and if we start it and run it you're going to see that indeed the classes now work or the objects now work and call the correct version of make sounds by default so there you go guys there is more information on object oriented programming using c-sharp and then the next part of the tutorial I think I'm going to use object-oriented programming to create a game think it's time for us to do something a little bit more fun so like always please leave your questions and comments below of the last till next time
Info
Channel: Derek Banas
Views: 87,632
Rating: undefined out of 5
Keywords: C# Tutorial, C#, Learn C#, C# Programming, C# Polymorphism
Id: ZzVxgJmLAsE
Channel Id: undefined
Length: 24min 13sec (1453 seconds)
Published: Sat Jan 28 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.