1 00:00:00,890 --> 00:00:04,550 Objects make your code organized and easier to understand. 2 00:00:04,970 --> 00:00:09,290 Object oriented programming means organizing your code around objects. 3 00:00:10,210 --> 00:00:14,950 And the purpose of object oriented programming is to write high quality code. 4 00:00:16,129 --> 00:00:19,280 In this lesson, you will create two car objects. 5 00:00:22,300 --> 00:00:24,220 Before we start, create a new folder. 6 00:00:24,220 --> 00:00:25,650 Call it section seven. 7 00:00:25,660 --> 00:00:32,380 Inside the folder, create a file named Main Java, and then make sure that your class has a main method. 8 00:00:38,510 --> 00:00:42,110 So your boss wants you to write code based on these requirements. 9 00:00:42,470 --> 00:00:47,300 And before you write a single line, step one is to identify the objects. 10 00:00:48,650 --> 00:00:52,670 We can define what an object is based on two criterias. 11 00:00:54,200 --> 00:00:56,810 An object is a thing that contains fields. 12 00:00:58,330 --> 00:01:02,920 And furthermore, an object is a thing that performs tasks. 13 00:01:04,470 --> 00:01:07,290 Take some time to remember these two bullet points. 14 00:01:09,630 --> 00:01:13,500 Now then, how many objects can you identify from this passage? 15 00:01:20,070 --> 00:01:23,010 I only count one type of object car. 16 00:01:24,320 --> 00:01:30,710 So based on the first bullet point, which describes an object as a thing that contains fields, car 17 00:01:30,710 --> 00:01:35,330 is a thing that contains the fields make price year end color. 18 00:01:36,850 --> 00:01:41,830 Going back to our criteria, an object is also a thing that can perform tasks. 19 00:01:42,010 --> 00:01:45,130 In this case, a car object can drive. 20 00:01:47,690 --> 00:01:49,680 So let's keep track of our progress. 21 00:01:49,700 --> 00:01:52,430 We identified one object, a car. 22 00:01:52,730 --> 00:01:56,810 A car is defined by its make price year end color. 23 00:01:58,460 --> 00:02:00,980 And furthermore, a car can drive. 24 00:02:03,850 --> 00:02:05,300 So that was step one. 25 00:02:05,320 --> 00:02:08,050 You identified one type of object car. 26 00:02:08,560 --> 00:02:10,840 Step two is to create a class. 27 00:02:13,140 --> 00:02:17,100 The class is a blueprint from which you can create an object. 28 00:02:19,540 --> 00:02:25,390 Previously we identified that every car is defined by its make price year and color. 29 00:02:26,050 --> 00:02:29,280 So based on these fields, we can create a blueprint. 30 00:02:29,710 --> 00:02:36,430 Our class, our blueprint, will declare that every single car needs to define a make price year end 31 00:02:36,430 --> 00:02:37,030 color. 32 00:02:40,580 --> 00:02:41,720 Inside your project. 33 00:02:41,720 --> 00:02:45,080 Create a new file called Car Java. 34 00:02:47,040 --> 00:02:52,350 And inside the car class, we declare that every single car needs to define a make. 35 00:02:54,530 --> 00:02:56,750 It needs to define a price. 36 00:02:58,270 --> 00:02:59,260 A year. 37 00:03:00,300 --> 00:03:01,620 And a color. 38 00:03:04,790 --> 00:03:05,660 And that's all. 39 00:03:07,440 --> 00:03:13,710 The car class is a blueprint, and from this blueprint we can create many car objects. 40 00:03:19,990 --> 00:03:22,780 And that's actually step number three from the car class. 41 00:03:22,780 --> 00:03:25,000 You will create car objects. 42 00:03:28,640 --> 00:03:30,650 The object will be of type car. 43 00:03:31,840 --> 00:03:35,380 Here we are creating a new object of the class. 44 00:03:39,290 --> 00:03:42,920 The variable automatically stores a reference to that object. 45 00:03:45,990 --> 00:03:49,650 And once I create an object, I can modify its fields. 46 00:03:56,790 --> 00:04:02,190 So what we'll do when our code is from the car class, from the blueprint that we just developed, we're 47 00:04:02,190 --> 00:04:04,320 going to create two car objects. 48 00:04:04,350 --> 00:04:07,050 The first object will be if type car. 49 00:04:07,950 --> 00:04:14,700 I will call the variable Nissan, and the Nissan variable will store a reference to a brand new object 50 00:04:14,700 --> 00:04:16,050 of the car class. 51 00:04:17,399 --> 00:04:20,810 This car object is automatically going to contain four fields. 52 00:04:20,820 --> 00:04:24,930 We can set the first field the make equal to Nissan. 53 00:04:26,550 --> 00:04:31,350 We can set its price equal to $10,000. 54 00:04:32,350 --> 00:04:34,030 We can set it's year. 55 00:04:34,960 --> 00:04:36,400 Equal to 2020. 56 00:04:37,900 --> 00:04:39,370 And we can set its color. 57 00:04:40,290 --> 00:04:41,530 Be equal to green. 58 00:04:45,900 --> 00:04:47,640 Let's visualize the runtime. 59 00:04:52,650 --> 00:04:59,820 Here we are creating a new object of the car class and the variable Nissan stores a reference to that 60 00:04:59,820 --> 00:05:00,720 object. 61 00:05:00,900 --> 00:05:05,490 And the object is fields by default are going to start at zero or null. 62 00:05:07,040 --> 00:05:09,890 Here we update that object as make to Nissan. 63 00:05:10,610 --> 00:05:13,520 Here we update it's priced at $10,000. 64 00:05:13,730 --> 00:05:17,060 Here we update that object says year to 2020. 65 00:05:17,060 --> 00:05:20,330 And here we update this object as color to green. 66 00:05:22,390 --> 00:05:24,340 And how easy was that? 67 00:05:27,320 --> 00:05:32,110 Using the same blueprint, I can create another object of the car class. 68 00:05:32,120 --> 00:05:34,550 I will call this variable dodge. 69 00:05:35,570 --> 00:05:41,150 And the Dodge variable will store a reference to a brand new object of the car class. 70 00:05:42,260 --> 00:05:47,990 This brand new car object is going to have a make that equals dodge. 71 00:05:50,380 --> 00:05:54,970 It's going to have a price that equals 11,000. 72 00:05:56,350 --> 00:05:58,420 It's going to have a year. 73 00:05:59,270 --> 00:06:00,950 Of 2019. 74 00:06:02,210 --> 00:06:04,850 And it will have a color of blue. 75 00:06:08,200 --> 00:06:12,910 Before we visualize the runtime, there is a text file inside of your resources folder. 76 00:06:12,940 --> 00:06:15,370 Please take some time to download it. 77 00:06:20,680 --> 00:06:25,690 The file that you just downloaded contains code that prints the fields of each object. 78 00:06:25,690 --> 00:06:31,630 So copy it into your main class and let us once again visualize the runtime. 79 00:06:33,360 --> 00:06:34,530 Press debug. 80 00:06:39,890 --> 00:06:43,280 Here we are creating a new object of the class. 81 00:06:44,410 --> 00:06:48,850 This variable, Nissan automatically stores a reference to that object. 82 00:06:48,880 --> 00:06:53,170 The object is fields by default start zero or null. 83 00:06:53,740 --> 00:06:56,020 Here we update that object as make. 84 00:06:56,020 --> 00:06:57,790 Here we update its price. 85 00:06:57,790 --> 00:07:01,120 Here we update its year and here we update its color. 86 00:07:01,950 --> 00:07:08,310 Here we're creating another object of the car class, and the variable dodge is going to store a reference 87 00:07:08,310 --> 00:07:10,110 that points to that object. 88 00:07:10,470 --> 00:07:17,520 The object is fields by default, start at zero or null, and once again, we update every single field 89 00:07:17,520 --> 00:07:18,900 in that object. 90 00:07:22,180 --> 00:07:27,970 At this point, we successfully created two objects of the car class, and finally, we can print the 91 00:07:27,970 --> 00:07:29,530 fields of each object. 92 00:07:29,680 --> 00:07:34,900 The first object, if I hover over the variable that points to it you can see, has a make of Nissan 93 00:07:34,930 --> 00:07:39,250 a price of 10,000 a year of 2020 and a color of green. 94 00:07:39,250 --> 00:07:41,200 So that's what's going to print. 95 00:07:43,360 --> 00:07:44,030 Beautiful. 96 00:07:44,050 --> 00:07:50,020 The second object, if I hover on the variable that points to it, has a make of dodge, a color of 97 00:07:50,020 --> 00:07:53,570 blue, a price of 11,000 and a year of 2019. 98 00:07:53,590 --> 00:07:55,270 Let's just press continue. 99 00:07:56,790 --> 00:07:58,830 And that is exactly what Prince. 100 00:08:01,340 --> 00:08:05,270 So to recap, you created two objects of the car class. 101 00:08:06,830 --> 00:08:12,320 A car class is a blueprint from which you can create concrete car objects.