1 00:00:00,210 --> 00:00:03,870 Before we start, did you try solving the workbook yourself? 2 00:00:03,900 --> 00:00:09,660 If not, please click the link in the resources folder and follow the instructions for this workbook. 3 00:00:11,030 --> 00:00:13,130 Welcome to Workbook 7.4. 4 00:00:13,160 --> 00:00:18,260 Task one is to add a copy constructor inside the person class. 5 00:00:18,560 --> 00:00:25,760 So while our normal constructor gets called when we pass in four parameters while creating an object, 6 00:00:25,790 --> 00:00:30,470 the copy constructor is going to get called when we pass in another object. 7 00:00:38,070 --> 00:00:44,280 So we're going to set the name of the current object that's being created equal to the field value from 8 00:00:44,280 --> 00:00:45,840 our resource object. 9 00:00:46,290 --> 00:00:53,040 We're going to set the nationality of the current object being created equal to the nationality of the 10 00:00:53,040 --> 00:00:54,180 source object. 11 00:00:54,390 --> 00:01:00,780 We're going to set the date of birth of the current object being created equal to the date of birth 12 00:01:00,780 --> 00:01:04,540 from the source object from the object that was passed in. 13 00:01:04,560 --> 00:01:10,920 And we're going to set the seat number equal to source dot seat number. 14 00:01:12,770 --> 00:01:13,310 Okay. 15 00:01:14,000 --> 00:01:16,160 Your code contains a person object. 16 00:01:16,160 --> 00:01:19,270 The person's twin is also flying with Java Airlines. 17 00:01:19,280 --> 00:01:25,070 Use this code to test your copy constructor so we can copy this over. 18 00:01:28,170 --> 00:01:33,890 Here, we're creating a new object of the person class, and we're going to initialize this object as 19 00:01:33,900 --> 00:01:36,000 fields using another object. 20 00:01:36,000 --> 00:01:38,460 Because one object is our argument. 21 00:01:38,460 --> 00:01:43,590 It's going to call the constructor that expects to receive one object as a parameter. 22 00:01:44,970 --> 00:01:51,810 And once we create this object based on the old object, then we can update the newly created objects 23 00:01:51,810 --> 00:01:56,700 as name equal to another value, and then update their seat number as well. 24 00:01:56,730 --> 00:02:01,740 Let us go ahead and print the other person's fields as well. 25 00:02:01,860 --> 00:02:05,010 Before we start visualizing the runtime. 26 00:02:05,670 --> 00:02:10,440 So I'm going to put one breakpoint over here, one over here. 27 00:02:12,410 --> 00:02:13,580 And should be good. 28 00:02:16,300 --> 00:02:23,120 So here we're creating a new object of the person class passing in all of these values into the constructor. 29 00:02:23,140 --> 00:02:28,490 That means the constructor that expects to receive four parameters is going to get called. 30 00:02:28,510 --> 00:02:37,030 Here we set every single field inside of the current object that's being created equal to a parameter 31 00:02:37,030 --> 00:02:37,570 value. 32 00:02:37,600 --> 00:02:40,030 So first we're going to start with the name. 33 00:02:40,030 --> 00:02:44,620 We set this name equal to Rand Slim. 34 00:02:45,100 --> 00:02:51,310 We set this nationality equal to the parameter that was passed in Canadian. 35 00:02:51,970 --> 00:02:59,500 We set this date of birth equal to the date of birth that was passed in 11111. 36 00:02:59,530 --> 00:03:06,190 We set this seat number equal to the seat number that was passed in. 37 00:03:08,520 --> 00:03:09,570 All right. 38 00:03:14,140 --> 00:03:17,560 The person variable stores a reference to this object. 39 00:03:17,560 --> 00:03:23,440 And if I step inside here, set seat number gets called for this object. 40 00:03:23,440 --> 00:03:28,840 And what it's going to do is set the seat number for the current object equal to the seat number that 41 00:03:28,840 --> 00:03:29,740 was passed in. 42 00:03:30,920 --> 00:03:32,120 All right, beautiful. 43 00:03:32,150 --> 00:03:34,670 I'm just going to continue to the next breakpoint now. 44 00:03:34,670 --> 00:03:39,170 So at this point, it printed all of this object as fields. 45 00:03:39,410 --> 00:03:43,040 Here, once again, we're creating a new object of the person class. 46 00:03:43,040 --> 00:03:48,830 But because we're only passing in one argument, it's going to invoke the constructor that expects to 47 00:03:48,830 --> 00:03:51,730 receive one parameter, the copy constructor. 48 00:03:51,740 --> 00:03:59,090 And so here this points to the object that we just created, the one that has everything as null and 49 00:03:59,090 --> 00:03:59,720 zero. 50 00:03:59,720 --> 00:04:07,550 So if I step over here, we're setting every single field inside of the current object equal to a field 51 00:04:07,550 --> 00:04:10,430 value from the source object that we passed in. 52 00:04:10,430 --> 00:04:14,330 So here we're updating the name based on the source object. 53 00:04:14,330 --> 00:04:17,420 Here we're updating the nationality based on the source object. 54 00:04:17,420 --> 00:04:21,769 Here we update the date of birth, and here we update the seat number. 55 00:04:25,940 --> 00:04:32,690 The variable twin stores, a reference to the object that we just created based on the field values 56 00:04:32,690 --> 00:04:33,820 of another object. 57 00:04:33,830 --> 00:04:38,690 And now what we're going to do is call set name for the twin object. 58 00:04:39,320 --> 00:04:41,150 So I'm going to step inside here. 59 00:04:41,150 --> 00:04:46,460 We're updating the name of the current object called this method equal to the following value. 60 00:04:50,360 --> 00:04:54,970 Stepping inside a gun set seat number gets called for this object. 61 00:04:54,980 --> 00:04:57,950 Here we update the seat number from 10 to 3. 62 00:05:01,510 --> 00:05:02,920 Now if I step over. 63 00:05:05,040 --> 00:05:10,680 Here we print the field values of the first object, and here we print the field values of the second 64 00:05:10,680 --> 00:05:13,290 object, and we are done.