‫Welcome back.
‫It's challenge time again and I hope you manage to check out the whole inheritance video and understand
‫what was going on and how to create the post and the image post and how they correlate to each other
‫so how they are connected to each other.
‫So that image post is the deriving class and post is the base class.
‫Because now the little challenge is to create a new class called Video Post, which is a driving class
‫with the property video URL and length.
‫So these are unique properties to that video post.
‫So just imagine we are still in this post type of thing and we have normal posts which are just text
‫then we have or which can be text but anything else as well.
‫Then we have an image post which also has an image URL and now what we want to have is a video post
‫where we actually have a video URL instead of an image URL and also the length of the video.
‫So these are the information that we want to have about every video post.
‫So now create the required constructors to create a video post adjusted to string method accordingly,
‫because we have seen in the image post that the two string method adds some more information based on
‫the information that was sent here already.
‫But we want to know even more about the video post than we know about the post.
‫So the just two string method then create an instance of video post.
‫So pretty much the same thing as we did here with the image post.
‫Just create one and then write it onto the console.
‫So maybe I should add that and use the two string method on it.
‫Then if you feel like a real challenge, then you can go ahead with the more advanced stuff.
‫So use a timer and a callback method here.
‫You can Google it.
‫You can simply yeah, you can try to do it yourself or you can Google it.
‫So the idea is that our video post has a method called play and one that is called stop, and when play
‫is called then they should write the current duration of the video.
‫So every single second there should be a tick writing at which second of the video we currently are.
‫And then one stop is pressed, the timer is stopped and there's also a text written stop that and that's
‫best with double P and yeah, that's pretty much it.
‫So play the video after creating the instance and pause it when the user presses any key.
‫Or maybe stop it when the user presses any key.
‫So by presses any key, I just mean you have a real line there.
‫So just keep it simple or read key better.
‫So just keep it simple and that what you see here will help you to create even video players later on.
‫So that's really just a basic concept and use timers here.
‫But as I said, this one is the more advanced part.
‫If you don't feel like doing it or if it's too difficult, then it's fine as well.
‫You can just check out the video, but I really advise you to check this out because it will teach you
‫how to use timers and callbacks by just googling it.
‫And it's best to just try yourself to experience how it's working and then you will definitely remember
‫it next time you want to use timers.
‫All right.
‫So pause the video now and try to do this challenge.
‫Already.
‫I hope you tried it.
‫First of all, we, of course, need a new class.
‫And it's going to be our video post.
‫And now that video post should inherit from post.
‫And here, as I said, we would have a different property or actually two different properties, which
‫we didn't have before.
‫And one of them is the video URL protected string video u r URL and it's a get and a set.
‫Then the same thing with the length protected int length with a capital l, it's best also get and set.
‫Next we need a constructor, so let's create a constructor here.
‫Public video post and I'm going to leave it empty again.
‫The same approach as we had in our image post here where we simply created this empty constructor because
‫neither constructor calls a base clause.
‫The one from the post will be called.
‫So that one here.
‫So if we have an empty video post, the post method will be called or the post constructor will be called
‫the default one.
‫But we also have one where we actually hand over the required information.
‫So for example, for a video post, what's interesting there, well, it's on one hand the title again
‫string title, then the username so sent by username, then the URL, string video URL and the bool
‫is published and even the length.
‫So in length is also useful here.
‫All right.
‫So quite a long constructor in terms of its parameters.
‫And now we, of course, need to use this dot and of course, this dot get ID or get next ID is going
‫to be required here as well.
‫So we want to not create an ID that is already there.
‫We want to go ahead and use the next ID.
‫And as you might recall, this get next ID is returning and increased or incremented current post ID,
‫which is a static variable.
‫So it's unique for this whole program, which means that it will be overridden with a new value or pretty
‫much we will simply get the new value and it's always going to be just one ID once.
‫So each ID is only going to be unique and that's really important here.
‫Then the title is going to be title and actually we can even go ahead and copy that from here because
‫it's the same approach.
‫Right.
‫And.
‫We can simply import it here and now we go ahead with our unique URLs.
‫So property video URL is a member of video post and actually I called it accidentally is published but
‫it should be as public with your post but not of post.
‫And that will be this video URL is going to be video URL and this length is going to be length.
‫So whatever is given over here as the argument should be taken into the object's properties which are
‫length video URL and of course the one that we inherited from post.
‫All right, so that's our constructor.
‫And now we need to have a two string method.
‫As we said, we would need a specific two string method.
‫And it's easiest if we just copy it from the image post and we override it again.
‫So public overwrite string to string.
‫So we override the method of our post class, which means we override this method here, which again
‫overrides from system that object.
‫So that's the beauty about inheritance you can override and override.
‫So now we have the string format and it might be a little different.
‫Well, different.
‫We can decide whether we want that or not.
‫We could send the length in here or we could leave it as is.
‫So the only difference would be not the image URL but the video URL are required.
‫And if you feel like it, you can add the length in here as well.
‫So if you want to for inform about that.
‫So maybe let's just do that.
‫Maybe it's easiest to just do that and that will be for done.
‫And in here I'm just going to add this dot length and I separate by comma.
‫So now in order to make it more readable, I'm just going to use a second line here.
‫All right, so that's our two string method.
‫And now we can go ahead into our posts or actually in our program class.
‫And in here we can create a video post.
‫Video post we're going to call it video post.
‫One is going to be a new video post and we could go ahead and do like that.
‫But then it would be an empty post, which is our ID zero post, like this one here, which just has
‫my first post as info.
‫It's public and it's by Dennis Panetta, but that's not very interesting.
‫So we are going to create one with our own unique values which will have a unique ID because we created
‫it that way.
‫So we can go ahead and here and I'm going to say this is a failed video and I uploaded it.
‫So Dennis Panetta uploaded it and you can find it under https video dot com slash fail video or something
‫like that.
‫It's just a fake ID or a fake link video dot com slash fail video.
‫And then as this is too long for one row, I'm just gonna put it over to the next one.
‫What we need now, if you check it out here, there are two overloads, so let's check out the overloads.
‫We need to have an additional information about the access or whether it's public or not.
‫And here we are again, public, and now we need the length.
‫So let's say this video is 15 seconds long or maybe 10 seconds long.
‫Ten is fine.
‫So just make it 10 seconds long.
‫And now.
‫That's pretty much it.
‫So we can already start our program and we should see the information about our video posts.
‫And of course, the more challenging part is still missing.
‫So that we are check out my new shoes that images dot com and actually we don't call the method yet
‫the two string method so maybe we should do that.
‫And next we need to call this video posts to string method.
‫All right.
‫So by the way, if you were wondering video that you can see, there are multiple methods here already.
‫And we didn't create those numbers.
‫They were just there.
‫And that is because we inherit from post, which again inherits from the system that object.
‫By the way, if you check it out for post one as well, post one dot, you can see those methods there
‫as well.
‫So equals get hashCode get type update.
‫So these methods, they are inherited from the system object class.
‫All right, so now we have the two string.
‫So now we of course need to put that into a console right line.
‫Actually, let's copy this code here and replace image post by video post one.
‫All right.
‫And now we can start it and there we are.
‫So one things for the birthday wishes, then three As you can see, the video post has a different ID
‫than our image post.
‫So we have one, three and two.
‫That is just the order in which we are writing them onto the console.
‫So if you check it out here at the video post, even though it's created after the image post we printed
‫out earlier, and that gives us this wrong order if you want.
‫But the order is not that important if you well, it's based on what you want to show.
‫So if you want to show the images based on the order that they were created or the posts in general,
‫based on the order that they were created, or you want to show the latest 1/1, those kind of things,
‫then the order of course matters.
‫But in our case it's really good that we have it that way.
‫So you can see that the ID is set up and at the moment when the object is created, so ID three and
‫ID two and another at the moment when it is called like here like the two string method.
‫So if you would like to have the correct.
‫Order for the order in which they are created would just go ahead like this.
‫And now we can see one, two and three.
‫So thanks for the birthday wishes.
‫Then our image with the new shoes than the fail video by Dennis Panetta.
‫And as you can see here, the length of ten is sent as well.
‫All right.
‫So that was part one.
‫And now the more advanced challenge, which is to create a timer and a callback method, which is called
‫every second and should inform us about the duration of the video or the current duration of the video.
‫And of course, we need to create the fields as required.
‫So we will need some additional fields in here.
‫For example, we will need to know at which point in the video we are.
‫So let's go ahead and actually let's create the fields before the properties.
‫So here properties and now let's create the memory fields.
‫And what is going to be a protected boolean is playing that is useful because if a video is playing
‫then you can pause it.
‫If it's not playing, then you can stop, start it.
‫So that's really important.
‫So if a video is already playing, you wouldn't want to start it playing.
‫That's why we have this helper boolean here and then we need a protected int cur duration.
‫It's zero now in order to call the timer.
‫We create a new method, let's create a public void play method and let's create the stop method as
‫well.
‫So public void, stop.
‫Now, what do we want to do here?
‫Well, first of all, let's just write something onto the console, like playing.
‫So we.
‫Okay.
‫The play was started or playing.
‫The video was started.
‫And in here I want to set the timer.
‫And first let's create the timer.
‫So we are going to need a timer here, timer, and I'm going to call it timer.
‫Now, in order to use timer, we need to use system dot threading.
‫That's the one that I need using system threading.
‫And now what we can go ahead and do is to initialize the timer whenever place pressed.
‫So timer is going to be a new.
‫Timer.
‫Timer callback should be the method that is called after.
‫What we need to state here, which I'm going to just say is no.
‫So as you can see, you need a timer callback, then you need an object, you need to do time and a
‫period and the due time is when it should start and the period should be how often should repeat it,
‫or in which time frame it should be repeated.
‫So the due time, I'm going to set it as zero and the period is 1000 milliseconds.
‫So now of course I need to have this timer callback method and I'm just going to go ahead and create
‫that.
‫And it should be a private method because I don't want anybody else to call it only this class itself.
‫So only void video posts should be able to call this timer callback and the timer callback while it
‫needs an object as its parameter.
‫And just going to say, oh, I'm going to call it oh, and now I can check if the current duration is
‫below the length of the video, then I want to go ahead and increase the current duration by one and
‫display onto the console.
‫So see w video at 0 seconds like this and then we need to have the current duration.
‫So what this will do is simply right onto the console.
‫And now in order to force the garbage collector for this video, we're just going to use GC Dot collection
‫so that will clean up any mess.
‫And by the way, else, if the current duration is not below the length, so that means that the video
‫is over.
‫We want to call the stop method.
‫So in here we're just going to call stop.
‫And now stop needs, of course, to be implemented.
‫So let's check out what we need in stop console dot right line and this is going to be stopped at and
‫then we just say at which second we stop the video and I'm going to say courage, duration.
‫And of course, we need to reset the current duration to being zero and stop the timer and stop the
‫time.
‫We call it this post method.
‫This one just cleans up the timer or stops the timer and resets it.
‫Now in order to make this even cleaner.
‫As I said, we have this is playing field and we can use that in our play method.
‫So we could say only if is playing this false.
‫So we use the exclamation mark at to start.
‫Only then I want to do this.
‫Only then I want to start playing.
‫Only then I want to initialize the timer.
‫And if it's not playing, then nothing should happen.
‫And here we can do the same.
‫So in the stop method we can check if is playing is true.
‫So only if the player is playing, we should be able to stop it.
‫All right.
‫So that's it for our video post class.
‫So now we should be able to check it out by going to our program and at one point start creating or
‫start playing the video.
‫So what I'm going to do is after I have created the video post, I'm going to play it.
‫And then as I said, I would like to leave the or let the user actually stop playing the video.
‫So how do we do that console that read key and afterwards I will call the stop method and in between,
‫of course I would like to inform the user about what you should do.
‫So I'm going to use C.W. here, console right line and I'm going to say press any key to stop the video.
‫All right.
‫And now we can check it out.
‫And see whether what we have built there actually works so that we are press any key to stop the video?
‫As you can see, the video has started and it's running and let's say I want to press any key and actually
‫I was too slow.
‫So the video went through and after 10 seconds it stopped.
‫And why is that?
‫Well, that's because our video is only 10 seconds long.
‫So we have this length property, which indicates how long the video is.
‫So let's say we create a different video and we want this video, or actually we changed the duration
‫of our video post to just being 5 seconds.
‫So let's just do that and run it again.
‫And then you will see after 5 seconds it will stop.
‫And there we are.
‫Video finished.
‫So now let's stop it manually.
‫Let's run it again.
‫There we are.
‫And let's press any key to stop the video.
‫And it's not working.
‫And that is because if we check the post Claus is playing is wrong.
‫And that's why is playing is never true.
‫So what we need to do here is to set is playing to true, of course.
‫And the same goes for our stop here.
‫So we say is playing is false.
‫All right, so now let's go back here and let's run it again.
‫And now the video is running and I post it or I stopped it and it says Stopped at four, and we could
‫even go ahead and change it to stop that 4 seconds like this.
‫And in order to have more time, as you saw, it was pretty close.
‫I'm going to give myself 10 seconds again.
‫So I drag it over.
‫Here I am and I stop it.
‫So I pressed the enter key.
‫Okey dokey.
‫So that was quite a challenge.
‫I hope you manage to do that, even the more advanced thing.
‫But what's really interesting, and I haven't touched upon it too much is this whole timer method.
‫So if you used it, then you understand what is going on or this time a class.
‫So there is a class called timer and you can use it in order to use timer methods or in order to use
‫a timer.
‫So what it does is you go ahead and create a new timer object and you can use a timer callback and the
‫timer callback allows you to be called or to execute some code each time that this duration is over.
‫So each time that the time frame that you assigned here so in my case 1000 milliseconds is over or this
‫period, then this timer callback will be called.
‫So it will be called in my case every single second.
‫That's why it says video at 1/2, 2 seconds and so forth.
‫That's pretty cool because now you can go ahead and create timer based methods or timer based programs
‫pretty much.
‫And you could create a whole timer where the user can play around with and do even more than we just
‫did here.
‫So the general idea is you create a new timer, you create a timer callback, which is this timer callback
‫method which needs an object that's important.
‫And then in here you can go ahead or within the curly brackets, you can go ahead and execute your code
‫regularly.
‫And that will be every so in so many seconds.
‫Then of course, the timer object has, if you look at it, a due time, which you can assign, which
‫is this one here, and a state which you can assign.
‫And I just use the state null.
‫All right.
‫So that was it.
‫And we are going to go ahead and see even more what we can do with inheritance and object oriented programming
‫in the next videos.
‫So see you there.