‫Welcome back.
‫In this video, it's all about inheritance.
‫And we're going to go ahead and create a class, which I'm just going to call post.
‫So a new class and it's going to be a post.
‫So imagine that being a social media post, such as a Twitter post or a Facebook post or whatever kind
‫of post.
‫And the idea is that I want to inherit from post and I want to create something like an image post or
‫later a video post, and those kind of things are going to enable us to understand the concept a bit
‫better of inheritance, and you can apply that onto different objects and different situations in the
‫future.
‫So first of all, a post has a variable which I'm going to make private static in current post ID.
‫So this one is going to be a field of that post and it's static.
‫So it's available outside of this post as well.
‫And it will be created once the application is started.
‫Right.
‫So what we can do is, first of all, create some properties.
‫And now the idea behind properties, as you know, is they give you some more information about the
‫object, right.
‫So in our case, if I create an object of post, there are certain information that we really want to
‫have.
‫For example, we want to know the ID well as a admin, as a programmer, not that much as a user.
‫And end user is not interested in the post ID, but as an admin or as the program you should be because
‫every post should be identified and it should have a unique ID.
‫So we're going to have IDs.
‫We need a title.
‫So for example, the Post has a title, maybe a description or a text, something like that.
‫Then we need to know who sent that post and whether it should be public or not.
‫Of course, there is a lot more to know about a post or there can be a lot more properties in a post,
‫but we are going to keep it simple for now.
‫So I'm going to create protected properties here and we have not used protected properties yet, but
‫in the end protect means that it can only be used by this class, by the post class and any deriving
‫classes, so classes which derive from post.
‫So I'm going to go ahead and create the first one which is going to be the ID, so int I d and we're
‫going to use get and set.
‫Then another property.
‫So I'm going to use Prop here and it should be a string and I'm going to call it title and as before
‫it should be protected.
‫Then another property and I'm going to use string again and this one will be send by user name.
‫So who sent it and it's going to be selected as well.
‫And finally, a property called is public.
‫So it's a boolean because it can only have two values.
‫So either it is public or it is not public.
‫So we have simply these properties, right?
‫So now we create a default constructor and there's something to know about the default constructor.
‫If derived.
‫If a draft class does not invoke a base clause constructor explicitly, the default constructor is called
‫implicitly.
‫That's why we create one here.
‫So we create a public post, and now that post will be well, because we don't get any parameters here.
‫It will be defined or we will set the values here, so ID will be zero.
‫Then the title is for example, my first post, or maybe with empty spaces.
‫First post like that then is it public?
‫Well, let's say true because we want to see it and then send by username well sent by me.
‫So then is new to in this case.
‫So whenever we create a post now it's just going to be this post here.
‫So it's going to be my first post, right?
‫Then I want to have another instance constructor that has several parameters.
‫So I want to create posts where I actually can define those things.
‫I don't define the ID, but I can define the title.
‫I can say whether it's public and whether it's sent by which username.
‫So you can imagine that let's say you write a little social media program and you want to have the capability
‫of posting, and then you need to send those information whenever you create a post.
‫So a user creates a post, it gets an automatically defined ID, then it gets an automatically defined
‫or it gets a defined title based on what the user has entered.
‫It is public based on what the user has ticked, whether he decided to make it public or not, and it's
‫sent by him.
‫So that's just data that is given over whenever a post is created.
‫So now let's create an instance constructor that has three parameters.
‫So I'm going to create public post and here I'm going to use the string title.
‫Bool is public and string send by username and in here I will set the ID and for that I will need something.
‫So I'm just going to set it to zero now.
‫But I will need an additional method which will always create a new ID for each post and we will see
‫how that works in a second.
‫So this title is going to be title.
‫This sent by username is going to be sent by username with a uncapped letter here and this dot is public
‫is going to be is public and not the property but this parameter.
‫So now I set that I don't want it to be zero.
‫The ID should be unique.
‫So the best way is to create a method which is going to be protected and it's called get next ID.
‫What that will do is it will simply return the next ID, which means current post ID and here we increment
‫before that.
‫So each time it's called, it's going to increment itself by one.
‫So this one here, this current post ID will be increased by one.
‫So now instead of setting zero here, we are going to call the next ID or get next ID method.
‫Now what else would you like to have in such a program where you can post something?
‫Well, I guess the capability of adjusting a post.
‫So for now we created a post and we would be pretty unhappy if we cannot change that post.
‫And well, that's something that does actually happen in I think what Snapchat or there you create a
‫picture and then it's there.
‫You cannot edit that picture, not sure whether you can edit the text, but here we want to be able
‫to edit the text so we will create a new method public void update and that should take in the title.
‫And the pool is public because if you think about it, what should the user be able to update?
‫He shouldn't be able to update the ID.
‫Neither should he be able to update the username because it's sent by him.
‫So he has a username and it shouldn't have impact onto the update of a post.
‫So in here we're just going to update the post and it means we're only going to change the title and
‫change the boolean, which is whether it's public or not.
‫And in here we simply say this title is going to be title, and this that is public is going to be public.
‫So that means, as we have here, a post.
‫A post is a blueprint.
‫Well, this class is a blueprint of any post instance.
‫So whenever we post something that is that self, that instance is the post.
‫And if we change its title, we change the title of that specific post.
‫And if we change, whether it's public or not, we change the publicity of that specific post.
‫So that enables us to update the title and it's publicity of an existing post.
‫So now the next one that I want to create is a virtual method override of two string that is inherited
‫from system dot object.
‫So System Dot object is a class that comes here in system.
‫So we can use that.
‫It's within C-sharp and we can simply use it.
‫And there is a method called to string or actually there from there we can call the two string method.
‫So.
‫System.
‫But object has a two string method and we want to override that because every single class so our post
‫class as well is inheriting from that object class.
‫So system that object is a class from which we can inherit.
‫So in here that object, for example, we can inherit from that or we do actually every class inherits
‫from that.
‫So now what we can do is we can override one of its methods.
‫So public override, string and two string.
‫As you can see, it already proposes this.
‫So it says returns a string that represents the current object that something that is within the object
‫class.
‫And as you can see there is something called return based to string.
‫So the base class has a two string method and if we just do that then it will then just pretty much
‫will mean nothing because we simply will use the two string method of the base class anyways.
‫But we can change that so we can adjust instead of returning whatever the base class would return.
‫We want to do our own thing, so we're going to use string format in order to format a string which
‫is going to have certain information.
‫So it's going to be the ID, it's going to be the title and who send it.
‫So I'm just going to use this kind of information like that.
‫So it's going to be this ID, so the ID, then this, the title, and finally this dot sent by username.
‫So that's what is going to return.
‫So whenever we use two string, it's not going to return simply some information about the object,
‫but it's going to return it in the standard format or in our format that we have created.
‫So this is our format.
‫All right.
‫So.
‫Let's create an object of that class.
‫So let's go ahead into our program and then here let's create a post.
‫So post post one is new post.
‫And here I can enter some information or I can leave it.
‫So if I leave it, that's the default constructor that will be called.
‫So that one here.
‫And if I don't, then a different one will be called.
‫So let's say I posted something.
‫Thanks for the birthday wishes.
‫So everybody wished me a happy birthday.
‫So I'm just going to say thank you very much and I want to make it true because I want my friends to
‫see it so it should be public.
‫And then Dennis Pan, Utah, is the one sending it.
‫Of course, we could have a user class which does all of the handling and we could simply check which
‫users logged in or whatever.
‫But for now we just use a hardcoded string here.
‫So it's going to be Dennis, Utah.
‫So that's a new post that we have here and now we can go ahead and write that onto the console.
‫So W and I'm going to say post dot to string and here of course the post one.
‫So our post instance, so this one is our post instance and I call the post one and it's going to write
‫something on the console and as we want to see what it is, we need to realign here.
‫So we just want to keep the console open so that we see what's happening there.
‫And then we are, as you can see, one, thanks for this birthday, birthday which is by Dennis Panetta
‫and this one is the ID of this post.
‫So that means that our post here.
‫This one gets a new ID and the new ID is going to be zero plus plus one or plus one, which means it's
‫just one.
‫So that's why we get this one here.
‫So the ID is one, then that's the post info and that's who send it by Dennis Pan.
‫So now we have a post and now let's go ahead and use inheritance.
‫Because so far, except for maybe this two string here, there is actually no inheritance.
‫So actually, just for you.
‫So you know what I said earlier, virtual method override of the two string method that is inherited
‫from system that object.
‫So now let's create a new class.
‫And that one will be image post because well, so far we can only post certain simple text, but in
‫the future we also want to post images.
‫And now that image posts should be a deriving class of our base class post.
‫Because this class here is a very simple class and we want to go ahead and inherit from it.
‫So in order to inherit from another class, you use colon and then you enter the name of the class you
‫want to inherit from.
‫So I want to inherit from post.
‫And what that allows me is to use all of its functionality.
‫That's something that's pretty cool because now I can use its properties, I can use its variables except
‫for its constructor here.
‫And yeah, so let's go ahead and do that.
‫And let's extend the image post.
‫So as you know, it derives from post and now it should have another property called image URL and to
‫constructors.
‫So that's what I want to add.
‫So first I need a property here and it will be a string called image.
‫You are l that will be the image url of the image post.
‫So let's say we post something and there is an image in it.
‫Then it's automatically or it should be an image post and it needs to have an image URL.
‫So a location where that image is stored and that can be on our servers or it could be simply on the
‫internet.
‫So whatever is there, we have this or you need to have this image URL for an image post.
‫Now let's create a very simple constructor, public image post
‫just like that, nothing else.
‫As you can see, there is nothing in there.
‫I don't want to do anything in here.
‫And then I create another post here or image post constructor.
‫But this time I want some parameters in here.
‫So it's going to be string title, string send by username, string image URL and actually u r l and
‫bool is public.
‫So I want to have all of those.
‫And now in the image post I want to set the ID again.
‫This dot id is equal to get next id and that's something that's pretty cool already.
‫Where the hell is this ID coming from?
‫And where is this get next ID coming from?
‫Well, it's coming from post.
‫So we are taking this method here, this protected method.
‫We can simply call it in here, we can use it as if it is ours.
‫So we don't even need to say something like post dot get next ID, we simply say get next ID and the
‫same thing for this ID.
‫So each image post has an ID, the same thing as the post itself.
‫And now that's true for the title as well.
‫So title is going to be title.
‫Then this dot send by username is going to be sent by username.
‫And finally, this is public is going to be is public.
‫Now, that's pretty cool.
‫We can use those things.
‫That's great.
‫And now we also have our own property, which is our image URL and we have that here.
‫So whenever an image post is created, the image URL has to be given over as an argument.
‫So we say this image URL is going to be the image URL.
‫So on one hand, we have the ones inherited from post.
‫So I'm gonna add that here.
‫And on the other hand, we have the one that is our own member.
‫So property image URL is a member of image post but not of post.
‫And that's something that's important.
‫So Post doesn't know about this image URL and it doesn't matter.
‫But our image post knows about all of the properties of our post, so that's inheritance.
‫Now, one little thing about this image post here.
‫So.
‫The constructors are required those two because neither constructor calls a base class.
‫So we don't use this based out calling the constructor of the base class and the default constructor
‫in the base class is called implicitly because we don't call it explicitly in here.
‫So neither in this one and nor in this one, the one from the base class, the constructor.
‫So this default constructor must be called or there must be a base constructor within our post.
‫That's why I created it.
‫Because you might have wondered, why do we need that?
‫Why is it there?
‫Well, it's required because we don't explicitly call it.
‫That's on.
‫And it will be implicitly called.
‫Right.
‫Great.
‫So that's that.
‫Let's go ahead into our program class and create one of those posts.
‫And I'm going to create an image post now and it's going to be a new image post and actually give it
‫a name.
‫I'm going to call it Image Post one.
‫It's going to be a new image post.
‫And I'm going to check out my new shoes because I bought some new shoes and they are freaking awesome.
‫And who sent it?
‫Well, Dennis Panetta was super hyped about his new shoes.
‫And of course, he needs to have a link for those images.
‫Well, he just uploaded the image, but the image itself has a link because it's stored somewhere in
‫our storage, so or in our servers.
‫So it's going to be a secure https and I'm just going to fake a link here.
‫I'm just going to call it images, dot com slash shoes and well, he made it public.
‫So it's true.
‫And let's put it onto the next line so it's easier to read.
‫So now we have our image post and what we can do with the image post now is to write something on the
‫console again to see what's going on.
‫And I'm just going to call image post 1.2 string and that's again something that's pretty cool.
‫So we call this two string method on our image post one.
‫And do you see each string method in here?
‫I don't.
‫And that's cool because we take the one from our post, so we inherit from post and now we take its
‫method, which is called two string.
‫And if we run that.
‫Actually, we need to stop running it.
‫Because we won't see it.
‫There is a reed and it's gone.
‫So we need to take this red line and post it right here.
‫So let's run it again and it says, Check out my new shoes by Dennis Panetta.
‫But where is the link?
‫I need the link.
‫Come on, give me the link.
‫Well, it's not there because the two string method of our post doesn't know about links.
‫It has no clue about links.
‫It doesn't care about links.
‫It doesn't need any.
‫So a little challenge for you.
‫And that's how we're going to finish the video for now.
‫Please create a two string method within our image post that also informs us about the link.
‫All right.
‫I hope you tried it.
‫So what I'm going to do is I'm going to keep it simple.
‫I'm going to copy the two string from here into our image post, and I'm going to change the format
‫just a little bit.
‫I'm going to add a little statement in here.
‫So too is going to be the link and three is still going to be the user.
‫So we need the link here.
‫So this dot image URL comma, so the order is ID title, the URL and then sent by user.
‫So if you do that, we override from the base clause.
‫So we override the two string from our post which overloaded from the system object.
‫So you can see you can override it and override it and so forth.
‫And we will see where the limit is or how we can limit that.
‫So now let's run it.
‫And we can see.
‫Check out my new shoes.
‫Here's the link.
‫Dennis Panetta.
‫So now i can click on the link in the console.
‫I can't.
‫But generally I could click on the link and I could see the image or my tool, my app, for example,
‫could read that link and show the image.
‫All right.
‫So that's a first glimpse into inheritance.
‫It was a long video.
‫I know that.
‫And inheritance is a lot more than that.
‫And we will look into a lot cooler things.
‫But first of all, we're going to go to a challenge in the next video.
‫And so see you in the next video.