8.3 Car Part 2
Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or
Already have an account? Sign In »

Video Transcription
00:00
hi and welcome back to part two of our inheritance lab.
00:04
Now, if you remember, we finished off or get and set methods, and now we want to do is we want to go into our Volkswagen and Audi class
00:14
and see how we can override our to string method. Now as a quick reminder. If you have forgotten what the two string method is or what it does, I'll just quickly tell you. So
00:26
the two string method returns a string representation of your object. Now that string represent representation by default is a hash code Value, which starts with the class name Hand has a bunch of hash rallies on the end.
00:42
Now that's not what we want to display or print to, like the consul over here. What we want to do is we want to display the car's details, and in order to do that, we need to override that to string method, and that is exactly what we're going to be doing over here right now.
01:00
So, first, in order to, uh, write it, I'm going to type public string and then to string, which is going to this operation here. If you click, enter on it. It is going to come out a complete
01:14
this first. And as you can see, we have our at override key word. And then we have our two string method. But obviously, we do not want to return this. We want to return the details of our car.
01:25
And when it comes to the details of our car or cars, this is where we're going to be using. That gets speeds that sorry to get speed that get year and get model method generally are get methods because those will get us that information.
01:40
So I'm going to type something like model
01:42
and then Colon plus get model
01:48
and then, uh, plus,
01:52
and I'm going to add a new line to nicely formatted
01:55
um,
01:56
year
01:59
and then Colon.
02:00
And then I'd like to add another plus
02:05
get speed.
02:07
So essentially, we're just the reason we're creating this to string, as I mentioned a few seconds ago, is to display our cars information and we decide how we want t o make our
02:20
information
02:22
Look in the consul. Sorry it wasn't Get speedy was get year,
02:25
and then I would like to add another new line.
02:30
And after that I will
02:34
finally,
02:36
um, at in the speed. So speed plus get speed.
02:42
And at the end, I would like to show the speeding kilometers per hour, so I'm just going to do km and then a slash on over here for anyone.
02:52
Okay, Perfect. So that is our to string method. Now we need to do something similar in
03:00
or the exact same thing pretty much in our walks, like in class
03:05
on that is over here.
03:08
And one more thing that I forgot to mention is that
03:12
this extents keyword over here is
03:16
why we are able to use the get model, get your and get speed method. This is where the inheritance comes in. Now, since Audi is a type of car in walks, wagon is a type of car. It makes perfect sense to use inheritance. And we're doing by that by using the extents, keyword and making use of the
03:36
get model.
03:37
And, um,
03:38
sort of get model gets beat and get year, um, methods in our car class.
03:45
So if you're wondering why this is in a single line, that's simply because java sometimes intelligence sometimes does that. And for that you just click on
03:53
the side of the first curly bracket over here, and then you should be good.
03:59
Okay?
04:00
It doesn't really mean mean anything. It's just inform anything. Okay? So going back to our
04:05
walks. Wagon cloth. So now we have our only class. We have our car class with everything. And now we need to do the same thing that we did our Audi class and walks working class. I'm gonna take this and copy it if I already haven't,
04:18
and then I will go ahead and pasted in here.
04:24
Okay, so we've pasted in here, and now we have our two string method for walks. Working class. Let's do something to differentiate between our Audi to string inner walks like into string.
04:36
Um, so I'm going to write details,
04:40
details
04:42
of your Audi,
04:46
Okay.
04:46
And then
04:48
that's at a bracket here. And then what we can do is
04:53
maybe at
04:56
this over here and see how this looks,
04:59
and then a new line capture. Okay, Perfect.
05:02
And I will take that part over here that I just added, and I'll add it to the two string method of the walks of Agon class.
05:11
So right over here.
05:16
Okay.
05:20
Sorry. one second.
05:28
Okay, Perfect.
05:30
All right, so now we're done with our walks, wagon class and our only class and our car class.
05:36
Now, what we need to do is we need to go into our tester.
05:41
Now, if you can see, I've already added some code into our tester class. And this is just for tthe e Audi. And now we would want to do something similar or exact. Same for walks, wagon. So take this off, copy it,
05:56
and then all
05:58
put it over here and set up. Audi will change into walks, Wagen, and we'll call our object V one
06:04
and then walks Wagen.
06:09
And here I will change a one to V one since we want is the name of our Volkswagen object.
06:15
And then I will
06:17
print out for anyone
06:19
on DDE.
06:21
Let's say Chetta
06:27
Okay, W v. Jetta Perfect.
06:29
And that set the year 2 2012
06:34
And the speed, too.
06:36
Uh, let's change the speed of Audi to two.
06:41
40
06:42
and then let's change this 1 to 200.
06:46
All right, so where you need to keep your eyes out is over here on the print statements
06:50
here. You need to watch out for the to string. The reason I'm saying that is because this is where our two string will
06:59
get called. And this is where we will see the result of or the details of our both of our cars.
07:05
So let's go ahead and run our program and see what happens.
07:11
So it'll take a few seconds to build,
07:14
and once it's done, building will see the result
07:16
perfect. So we can see over here is details of your Audi,
07:21
and the model is an audio or eight years. 2018. Speed is to 40 kilometers an hour. And then here there are the details of our Jetta or Volkswagen. But obviously that is incorrect. So we'll go back and
07:34
change this to walks.
07:38
Sorry. I forgot to change.
07:41
Um,
07:42
the name
07:44
so
07:45
oval surviving. All right. And then let's run it again. And then this should be changed over here. Perfect. Now it's changed,
07:51
and we have the corresponding details of our work's fighting car here
07:57
and our Audi car right here.
07:59
Okay, so now going back to our electric
08:03
in this lab, we discussed the implementation of inheritance and how inheritance works through an example of a general car class and sub cars or an Audi in a Volkswagen to see how we can inherit, get in, said methods and make use of them and override the to string method to display
08:22
our objects in our own,
08:24
um, format of choice.
08:26
So
08:28
coming up next, we're going to be starting Marshall Line, which is the final module, and is on polymorphism.
08:33
And, as always, thank you so much for watching this video, and I would love to answer any of your questions. So feel free to connect with me via Ellington and I look forward to seeing you in Module nine.
Up Next
Similar Content