8.2 Car Part 1
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
Already have an account? Sign In »
4 hours 19 minutes
Hi and welcome back to the course. I hope you guys are doing well. And if you remember in the last video lecture, we went over one of the components of old p called inheritance and what it is all about. And in this lab, we are going to be making use of inheritance by creating a program that is based on the idea
And we will have a few classes that will take advantage of a more general class to demonstrate this idea of inheritance.
Now, at this point, I'd like you to go ahead and switch over to intelligence.
And my intelligent over here has four classes. We have a car class and Audi class of walks, value class and then a tester class.
Now, you can probably tell from the names that we are referring to cars over here and then our tester classes just to test our program in our coat.
Now, I would like you to go ahead and create these classes by going on file
foil, uh, new and then Java class. And here you can type in whatever you want and click. Okay. And you'll be good to go
once you have act,
um, you can go ahead and add these comments if you'd like, but they're just a quick guide to what we're going to be doing in our car class.
So let's begin making this class over here.
So one of the very first things that we can see is that
we have a comment that says, Store the speed.
we want to use encapsulation in our class and we want to store the speed, but we want to keep it private, and we would like to do the same for the year and the model because there is no need for anyone else to know
the year speed or model of our car. And using encapsulation generally is a good practice and programming.
So I'm gonna go ahead and create a private variable called Sir Private integer called Aunt
and that we're going to do by doing this.
And then we will do the same for year private into year.
And finally we would like to store the model version, which is a string so we will say private string
and then let's call it a model
and always remember to keep all your names relevance to what you're trying to accomplish
in your program.
Okay, so we do not need a constructor for this lab since where you have, we're gonna be creating, get and set methods for these variables over here. So let's get started on the get methods.
So first, I would like to get the speed
car. And I do that by making a get method called get speed and the reason we have to make these get and set methods again just give you a quick reminder is that since these variables air private, in order for us to access these variables outside of the car class, we need to have these get and set methods get methods will allow you to
get the values or
the information from these methods, or so from these variables. And the set methods will allow you to set or modify or change whatever you may call it the values of these variables.
So since I am getting the speed and my return, time is int, I would like to return speed,
return speed, and we will do the same for a year so public
So you get year and then
we are going to be returning
the year, so return
you're perfect. And let's do the same for
model so public and string. In this case, since we're returning a string and model is a string, we will name it, get model
and then returned
And now we have our get methods. And since we're done with our get methods, let's move on to our set methods.
So let's create a, um
actually, let's go up right here and let's create a set speed methods of public and
And I would like to specify,
uh, the amount, so I'm going to pass in a variable called speed, which is
the speed that I want to set for my car.
So then I'm going to say
or this starts speed, which is referring to speed and this class speed very well in this class to speed.
And then I will go ahead and
do the same for and sort. This is going to be a void method. Since we aren't returning anything, we're actually setting something,
and then I'm going to do the same for get year and get model. So forget year what we would like to do is, um,
create another segmented,
void. Since we're not returning anything
and then in here, what we can do is you can say, in
on over here, we can say,
um, this start
And let's also do that for model
Boyd Set model
Okay, Perfect. So that is what we have done for our
car class. We have our three get methods. We have our three set methods for these three private variables over here.
And now what we need to do is we need to go into our Audi and walks back in class, and we need to override a default to string method now, not going to say too much about it right now, so I'll pause the lab over here, and we will continue in part two of this lab.
Thank you for watching. And I look forward to seeing you in part two