‫Welcome back.
‫In this video, we'll learn about objects.
‫An object is called an instance of a class.
‫We created this class called Car.
‫Now, suppose that you want to create 2022 car or a Porsche or a red car or whatever the attribute is.
‫These cars are called objects from the class car.
‫I need to create a car with 2022 year or 2023.
‫We start by creating the class name.
‫Here is the syntax of object creation.
‫We start with the class name, followed by the object name equal to new.
‫And then we start with the class name again.
‫This is the general syntax of creating an object in Java.
‫So I'll follow this syntax to create an object from a car, starting with the class name.
‫What is the class name here?
‫It's a car.
‫So I'll start with the car, the object name, I'll name it as red car equal to new keyword.
‫We have used the new keyword along with the constructor of the class to create an object.
‫Constructors are similar to methods and have the same name as the class.
‫In the next video we'll talk about the constructors, but now folks with me on how to create objects
‫in Java.
‫The class name, the object, name equal and then new keyword.
‫And then we specify the car, the class name, followed by these parentheses in this way, we've created
‫an object of the class car called Red Car.
‫Now, if I type red car, I use this object and put the dot operator.
‫I can access the speed, the year, the accelerate and the brake methods.
‫So these methods, these are two methods and these are two attributes.
‫We can use the name of objects along with the dot operator to access members of a class.
‫So for example, if I need to define the speed of the red car to 180km/h and the red car dot year is
‫equal to 2023.
‫So here I am defining these attributes and setting the attributes of the red car to these values.
‫Also, I can call the functions like accelerate, then call the brake method.
‫In this example, we created an object called Red car.
‫From the class car.
‫We access the speed and set it to 180.
‫We accessed the field year and set it to 1023 and we call the method accelerate, then brake.
‫So in this example, it's very helpful and I want from you to understand well and write it down on your
‫copy book because it's the building block to understand the objects and classes.
‫Now let's test the methods and the attributes.
‫Let's go down and here the accelerate.
‫I'll print out the method.
‫Then you speed your your new speed is and I'll get the speed attribute.
‫Also, when I call the brake, I need to get the speed.
‫Let's go up and I'll accelerate two times and then I'll break.
‫Let me run and see.
‫And here we go.
‫This is the output.
‫So let's explain.
‫Red car speed is initialized to 180.
‫So whenever you turn on the car, your your speed will be 180 directly.
‫Then you accelerate.
‫So we increase the speed by ten.
‫Here is 190.
‫Then we accelerate again by ten the speed.
‫So it becomes 200.
‫Then we break, we remove five.
‫We decelerate by five kilometers per hour.
‫So our new or the current speed is 1.95.
‫This is the example on objects.