1 00:00:00,760 --> 00:00:03,610 Child classes can inherit from a parent class. 2 00:00:06,810 --> 00:00:10,260 In this lesson, two classes will inherit from a parent class. 3 00:00:14,950 --> 00:00:19,450 First, open up the folder for this lesson by following this path in your course resources. 4 00:00:24,700 --> 00:00:30,460 And now from the requirements, we can identify what seems to be three objects, store shirt and pants. 5 00:00:32,320 --> 00:00:36,460 We're not going to criticize or object and we're not going to be writing any unit tests just to keep 6 00:00:36,460 --> 00:00:37,810 the focus on inheritance. 7 00:00:39,670 --> 00:00:42,730 OK, let's get right to it, so first we have to create a shirt class. 8 00:00:48,920 --> 00:00:53,600 The requirements tell us that shirt has Fairfield's private string size. 9 00:00:57,660 --> 00:00:59,000 Private double price. 10 00:01:02,160 --> 00:01:03,330 Private string color. 11 00:01:09,850 --> 00:01:11,440 And private string brend. 12 00:01:15,640 --> 00:01:20,080 And naturally, we would create a constructor that receives four parameters. 13 00:01:33,160 --> 00:01:38,590 OK, now you want to set every field in the object that's being created equal to a parameter this size, 14 00:01:38,590 --> 00:01:43,390 equal size, the stock price equals price, the stock color. 15 00:01:44,590 --> 00:01:45,400 Equals color. 16 00:01:47,450 --> 00:01:49,460 This thought brand equals brand. 17 00:01:53,030 --> 00:01:55,520 For now, don't do anything else for this class. 18 00:01:55,550 --> 00:01:57,530 We'll move on and create a pince class. 19 00:02:01,040 --> 00:02:02,510 Public class pant's. 20 00:02:06,540 --> 00:02:10,740 And instead of a size field, the pants class defines a waste field. 21 00:02:12,740 --> 00:02:17,870 The remaining fields are identical to the ones for shirt, so could we just copy them over? 22 00:02:19,010 --> 00:02:21,350 You know how I feel about copying and pasting code. 23 00:02:21,380 --> 00:02:22,920 You should never do it before. 24 00:02:22,970 --> 00:02:24,370 Now I'll entertain it. 25 00:02:24,470 --> 00:02:25,370 Let's go through with it. 26 00:02:26,120 --> 00:02:29,360 And I guess we could also copy and paste the constructor. 27 00:02:34,790 --> 00:02:38,870 Change shirt, the pants, and instead of shrink size put into waist. 28 00:02:42,710 --> 00:02:47,390 Now, on that very same note, never, ever copy and paste code. 29 00:02:48,820 --> 00:02:52,390 If you copy and paste code, that means you're doing something wrong. 30 00:02:52,870 --> 00:02:55,330 What we did here is really, really bad. 31 00:02:55,930 --> 00:03:01,750 If you decide to copy and paste now, then you would end up copying and pasting together as the setters. 32 00:03:02,080 --> 00:03:03,790 Let me generate them really quick. 33 00:03:05,080 --> 00:03:07,530 And then, heck, why not copy and paste those as well? 34 00:03:13,420 --> 00:03:18,850 OK, now I hope you see my point, never copy and paste code, it's really, really bad practice. 35 00:03:20,200 --> 00:03:22,800 This is why inheritance is so important. 36 00:03:23,290 --> 00:03:29,590 Inheritance makes code reuseable shirt and pants need to inherit from a parent class. 37 00:03:32,900 --> 00:03:35,620 Shirt and pants share similar fields. 38 00:03:41,590 --> 00:03:47,470 Instead of defining the same fields in each class, they should inherit common fields from a parent 39 00:03:47,470 --> 00:03:48,040 class. 40 00:03:55,290 --> 00:04:01,770 In other words, shirt and pants are children of the product class inside models create a product class 41 00:04:01,770 --> 00:04:03,000 product Java. 42 00:04:06,310 --> 00:04:10,030 The product class is going to define fields that shirt and pants have in common. 43 00:04:14,440 --> 00:04:15,970 It's a private double price. 44 00:04:21,160 --> 00:04:22,390 Privates Tranquila. 45 00:04:25,710 --> 00:04:26,970 Private string brand. 46 00:04:32,770 --> 00:04:34,990 And now you can remove these fields from Pentz. 47 00:04:36,730 --> 00:04:42,820 We'll get errors here, so remove the field updates inside the constructor and delete all the letters 48 00:04:42,820 --> 00:04:43,480 and setters. 49 00:04:48,710 --> 00:04:50,080 We'll do the same thing for shirt. 50 00:04:51,670 --> 00:04:53,740 Remove the field updates in the constructor. 51 00:04:55,870 --> 00:04:57,490 As well as the getters and setters. 52 00:05:00,410 --> 00:05:04,430 Now, a class that extends another class inherits from its. 53 00:05:07,080 --> 00:05:10,440 Pants and shirt must extend the product class. 54 00:05:32,910 --> 00:05:38,760 And that's really it now the pants class and shirt class are inheriting from their parent class product. 55 00:05:42,460 --> 00:05:47,830 OK, now how can we make sure that pants and shirt are actually inheriting from the product class easy 56 00:05:48,250 --> 00:05:51,010 inside main an object of the shirt class. 57 00:05:53,650 --> 00:05:55,090 Percent a size of small. 58 00:05:57,760 --> 00:05:59,380 A price of five ninety nine. 59 00:06:03,140 --> 00:06:04,190 A color of blue. 60 00:06:08,210 --> 00:06:10,810 And a brand of will say job over time. 61 00:06:13,520 --> 00:06:14,690 Now, put a break point. 62 00:06:15,810 --> 00:06:16,920 And run the debugger. 63 00:06:29,950 --> 00:06:35,710 It creates a new object of the class, you can see the object includes the field inside the child. 64 00:06:35,740 --> 00:06:36,580 Class size. 65 00:06:38,610 --> 00:06:43,590 But wait a second, it also includes the fields that an inherited price color and bred. 66 00:06:49,590 --> 00:06:54,180 Let's make sure Pence is inheriting from product create a new object of the pants class. 67 00:07:06,750 --> 00:07:11,910 Passing a waste of 30 to a price of twenty four point ninety nine, a color of blue. 68 00:07:14,220 --> 00:07:16,770 And a brand of, let's say, Javor Klein. 69 00:07:23,020 --> 00:07:23,850 Run the debugger. 70 00:07:28,590 --> 00:07:30,900 It creates a new object of the pants class. 71 00:07:33,070 --> 00:07:37,510 You can see that the object includes the field inside the child class waste. 72 00:07:39,910 --> 00:07:46,480 But wait, it also includes the fields that an inherited price, color and bread, that is inheritance. 73 00:07:48,000 --> 00:07:54,450 All I want you to take away from this lesson is this when two classes share common fields, never defined 74 00:07:54,450 --> 00:07:56,040 the same fields in each class. 75 00:07:58,360 --> 00:08:02,320 Instead, each class should inherit common fields from a parent class. 76 00:08:05,950 --> 00:08:08,530 Child classes can inherit from a parent class. 77 00:08:11,030 --> 00:08:16,020 When two classes share common fields, never defined the same fields in each class. 78 00:08:16,400 --> 00:08:19,850 Instead, they should inherit common fields from a parent class. 79 00:08:23,810 --> 00:08:29,960 Shirt and pants shared similar fields, but instead of defining the same fields in each class, they 80 00:08:29,960 --> 00:08:33,049 inherited common fields from a parent class.