1 00:00:00,750 --> 00:00:01,680 ‫Welcome back. 2 00:00:01,710 --> 00:00:09,540 ‫In this video we are going to talk about has a relationship because so far what we have seen in inheritance 3 00:00:09,540 --> 00:00:11,790 ‫is an is a relationship. 4 00:00:11,790 --> 00:00:17,550 ‫So here, for example, I'm going to get rid of the SEALs here from last video. 5 00:00:17,550 --> 00:00:21,860 ‫So a car or a BMW better set? 6 00:00:21,870 --> 00:00:25,440 ‫A BMW is a car. 7 00:00:25,770 --> 00:00:30,120 ‫So it's an is a relationship between BMW and car. 8 00:00:30,360 --> 00:00:31,350 ‫The same for Audi? 9 00:00:31,350 --> 00:00:36,540 ‫An Audi is a car, an MX three is a BMW. 10 00:00:36,540 --> 00:00:40,500 ‫So that's pretty much the general idea of inheritance. 11 00:00:40,500 --> 00:00:44,310 ‫But there is also something called an has a relationship. 12 00:00:44,310 --> 00:00:51,630 ‫So let's say we would like to have an additional information set for our car in general for every car 13 00:00:51,630 --> 00:00:52,290 ‫there is. 14 00:00:52,500 --> 00:01:00,630 ‫So what we can do there for is to create an additional class and I'm going to call this class car info 15 00:01:00,960 --> 00:01:06,420 ‫or actually let's go ahead and create that maybe even car ID info. 16 00:01:06,420 --> 00:01:16,260 ‫So in order to identify every single car, let's create a new class and call it one car ID info. 17 00:01:17,040 --> 00:01:29,370 ‫So this class here has a inheritance or relationship, so pretty much a BMW has a car ID info or a car 18 00:01:29,370 --> 00:01:31,470 ‫has a car ID info. 19 00:01:31,470 --> 00:01:34,980 ‫So in here I just want to have some properties. 20 00:01:34,980 --> 00:01:40,320 ‫For example, an integer called ID num. 21 00:01:40,500 --> 00:01:48,930 ‫So this one will be initialized with zero and otherwise this is just going to be the ID, which is a 22 00:01:48,930 --> 00:01:51,750 ‫unique identifier, not a number. 23 00:01:51,750 --> 00:01:55,560 ‫So it's pretty much every car has this number and it's unique. 24 00:01:55,560 --> 00:02:00,810 ‫So car one has the ID of zero, the second car has the idea of one and so forth. 25 00:02:00,930 --> 00:02:05,580 ‫And also every car should have an owner. 26 00:02:06,180 --> 00:02:12,720 ‫So public string owner and actually I'm going to use a property as well. 27 00:02:12,720 --> 00:02:25,710 ‫So prop string owner and here also get and set and if it doesn't have an owner, then the standard statement 28 00:02:25,710 --> 00:02:26,820 ‫should be no owner. 29 00:02:26,820 --> 00:02:29,070 ‫So let's say the car was just produced. 30 00:02:29,100 --> 00:02:34,260 ‫Of course, you could argue that this car is still owned by the company that produced it, let's say 31 00:02:34,260 --> 00:02:42,510 ‫by a BMW or by Audi or by any other car producer, and otherwise it has an owner. 32 00:02:42,510 --> 00:02:46,350 ‫So in that case, let's say it has no owner, but otherwise it has an owner. 33 00:02:46,350 --> 00:02:53,390 ‫So this car ID info is something that I can create an instance of in my car class and then I can use 34 00:02:53,400 --> 00:02:57,480 ‫it in any other of the driving classes as well. 35 00:02:57,480 --> 00:02:59,280 ‫So let's go ahead and do that. 36 00:02:59,410 --> 00:03:14,670 ‫Let's create in here a has a relationship has a relationship which means a car has a protected car ID 37 00:03:14,670 --> 00:03:24,090 ‫info and I'm going to call that one car ID info, which is a new car ID info. 38 00:03:26,970 --> 00:03:35,280 ‫And now in my car I'd InfoQ I can simply set things so we could go ahead and create setters in here 39 00:03:35,280 --> 00:03:36,630 ‫and all of those kind of things. 40 00:03:36,630 --> 00:03:42,480 ‫But I'm just going to use that right in here because the beauty about having properties is that I can 41 00:03:42,480 --> 00:03:50,820 ‫actually access them in here so I can create a method called set car ID info. 42 00:03:50,820 --> 00:04:04,940 ‫So that will do is it will simply set the car ID ID that one here dot id num two id num. 43 00:04:04,980 --> 00:04:08,970 ‫And now you're wondering where is this ID name coming from, this second one here? 44 00:04:08,970 --> 00:04:14,400 ‫Well, it's an integer that I'm going to deliver whenever I'm setting. 45 00:04:14,490 --> 00:04:16,950 ‫So this is going to be a property here. 46 00:04:16,950 --> 00:04:19,350 ‫And another one that I need is the owner. 47 00:04:19,350 --> 00:04:20,760 ‫So string owner. 48 00:04:21,390 --> 00:04:27,210 ‫So when I set the car ID info, I want to have these two information and this information. 49 00:04:27,210 --> 00:04:32,490 ‫And in here I also can say car ID info is going to be here. 50 00:04:32,490 --> 00:04:40,800 ‫It's the owner is going to be the owner, but here actually not capitalized if possible. 51 00:04:40,800 --> 00:04:47,580 ‫So we have the owner and it will be the car owner whenever we use the set car ID info method. 52 00:04:47,580 --> 00:04:55,590 ‫So let's go ahead and get the car ID info as well by creating a new method public void, get car ID 53 00:04:56,280 --> 00:05:01,620 ‫info and that one will simply write something onto the console. 54 00:05:01,620 --> 00:05:16,020 ‫Something like the car has the ID of and here I'm going to use zero and is owned by and here I'm going 55 00:05:16,020 --> 00:05:17,820 ‫to use the one. 56 00:05:17,820 --> 00:05:31,050 ‫So here I'm going to say car ID info dot ID number and car ID info dot owner. 57 00:05:33,560 --> 00:05:41,660 ‫So what that allows me to do is to adjust the card info for every single car that I have, no matter 58 00:05:41,660 --> 00:05:46,670 ‫if it's just a car, if it's a BMW, if it's an Audi three or whatever. 59 00:05:46,820 --> 00:05:52,160 ‫Because this class has now a has a relationship here. 60 00:05:52,160 --> 00:05:59,600 ‫So we simply can go ahead into the program class and in here we can add some information. 61 00:05:59,600 --> 00:06:09,530 ‫So let's say we want to add some info to our BMW and oh, is that three? 62 00:06:10,010 --> 00:06:11,960 ‫So let's go ahead and do that here. 63 00:06:13,010 --> 00:06:20,930 ‫BMW Z three is going to get a car ID info, which is let's say this one has the idea of one, two, 64 00:06:20,930 --> 00:06:25,940 ‫three, four and the owner is going to be Dennis on Utah. 65 00:06:27,140 --> 00:06:35,570 ‫And now there is the Audi a three and this one is going to get a car ID info of one, two, three or 66 00:06:35,570 --> 00:06:39,590 ‫five and the owner is going to be Frank White. 67 00:06:41,630 --> 00:06:51,320 ‫And now we can go ahead and get those IDs, get car ID info for the BMW and also for the Audi three, 68 00:06:51,350 --> 00:06:53,270 ‫get car ID info. 69 00:06:53,780 --> 00:07:02,090 ‫So now if you call that or if we run that code, we will see that the additional info is available at 70 00:07:02,090 --> 00:07:02,840 ‫our car. 71 00:07:02,840 --> 00:07:07,430 ‫So that's a way that you can simply add information to an object. 72 00:07:07,430 --> 00:07:12,500 ‫So you can see the car has the idea of one, two, three, four, and it's owned by Dennis Panetta and 73 00:07:12,500 --> 00:07:17,750 ‫the car has the ID of one, two, three, five and is owned by Frank White. 74 00:07:19,170 --> 00:07:19,710 ‫All right. 75 00:07:19,710 --> 00:07:26,220 ‫So that's generally how you can go ahead and add information. 76 00:07:26,220 --> 00:07:31,320 ‫So that's an aggregator for our car. 77 00:07:31,920 --> 00:07:36,330 ‫And it's it has a relationship way of approaching things. 78 00:07:36,330 --> 00:07:36,930 ‫Great. 79 00:07:36,930 --> 00:07:39,570 ‫So that's pretty much it about has a relationships. 80 00:07:39,570 --> 00:07:41,100 ‫So see you in the next video.