1 00:00:00,000 --> 00:00:04,887 [MUSIC] 2 00:00:04,887 --> 00:00:06,800 Hi There. This is James. 3 00:00:06,800 --> 00:00:12,120 In this C# practice session, you'll practice class inheritance using C#. 4 00:00:12,120 --> 00:00:17,370 It reinforces what you learned in stage three of the C# objects course. 5 00:00:17,370 --> 00:00:20,470 If you find this practice session too challenging to complete, 6 00:00:20,470 --> 00:00:22,640 you might need to review that course. 7 00:00:22,640 --> 00:00:24,760 See the teacher's notes for a link. 8 00:00:24,760 --> 00:00:28,690 This practice session is the third in a series of sessions 9 00:00:28,690 --> 00:00:31,875 where you'll build out a media library console application. 10 00:00:31,875 --> 00:00:35,560 Step-by-step you'll add features to the program. 11 00:00:35,560 --> 00:00:39,640 Eventually, you'll be able to use C# to add, list, and 12 00:00:39,640 --> 00:00:42,100 search for items like albums, books, and 13 00:00:42,100 --> 00:00:46,430 movies, whatever you want to have catalogued in your media library. 14 00:00:46,430 --> 00:00:51,180 In previous practice sessions, we defined the attributes and behaviors for 15 00:00:51,180 --> 00:00:56,290 our program's media type objects by adding class fields and methods. 16 00:00:56,290 --> 00:01:01,330 In this practice session, we'll see how we can use class inheritance to extend 17 00:01:01,330 --> 00:01:04,810 to all of our immediate type classes the behaviors 18 00:01:04,810 --> 00:01:09,330 that we currently have defined in just a single media type class. 19 00:01:09,330 --> 00:01:13,400 Go ahead and open your workspace from the previous practice session. 20 00:01:13,400 --> 00:01:17,080 Or you can open the workspace that I've attached to this video. 21 00:01:17,080 --> 00:01:22,210 If you want, you can download the project files in order to use an external editor 22 00:01:22,210 --> 00:01:25,020 or an IDE like Visual Studio. 23 00:01:25,020 --> 00:01:30,050 In the last practice session, we added behaviors to our album media type class. 24 00:01:30,050 --> 00:01:32,220 But really, these behaviors or 25 00:01:32,220 --> 00:01:35,910 methods should be available on all of our media types. 26 00:01:35,910 --> 00:01:40,640 We could copy and paste these methods to each of our other media type classes. 27 00:01:40,640 --> 00:01:43,990 This wouldn't be too much work given that we currently 28 00:01:43,990 --> 00:01:46,720 only have two other media type classes. 29 00:01:46,720 --> 00:01:50,620 But what if we had five or ten media type classes? 30 00:01:50,620 --> 00:01:54,270 Not only would this be a lot of extra work, in general, 31 00:01:54,270 --> 00:01:57,120 we should strive to avoid code duplication. 32 00:01:57,120 --> 00:02:00,340 Unnecessarily duplicating this code could make 33 00:02:00,340 --> 00:02:03,760 future maintenance on these classes more difficult to manage. 34 00:02:03,760 --> 00:02:06,910 So let's practice using object oriented programming 35 00:02:06,910 --> 00:02:11,660 in order to keep our code clean, concise, and easier to maintain. 36 00:02:11,660 --> 00:02:16,470 Remember from the C# objects course that classes define what it means to be 37 00:02:16,470 --> 00:02:18,150 in a particular classification. 38 00:02:18,150 --> 00:02:19,790 The album, book, and 39 00:02:19,790 --> 00:02:24,520 movie classes are all representations of their real life counterparts. 40 00:02:24,520 --> 00:02:28,950 They're also subclasses of a more generalized classification. 41 00:02:28,950 --> 00:02:32,190 Specifically, they're all media types. 42 00:02:32,190 --> 00:02:35,590 We need to create a media type base class. 43 00:02:35,590 --> 00:02:40,160 Then we can update our media type subclasses to inherit from the media 44 00:02:40,160 --> 00:02:44,060 type base class, allowing them to inherit the attributes and 45 00:02:44,060 --> 00:02:48,080 behaviors from the more generalized base class. 46 00:02:48,080 --> 00:02:52,450 For your first challenge, you'll add a MediaType base class to your program. 47 00:02:52,450 --> 00:02:57,630 Within that class, you'll define a public filed named Title of type string, 48 00:02:57,630 --> 00:03:01,920 and you'll add a constructor that defines a parameter named title, 49 00:03:01,920 --> 00:03:06,430 again of type string, that you'll use to initialize the Title field. 50 00:03:06,430 --> 00:03:10,510 After adding the base class, you'll update your existing media type classes to 51 00:03:10,510 --> 00:03:13,320 inherit from the MediaType base class. 52 00:03:13,320 --> 00:03:16,400 Then, from each of these media type subclasses, 53 00:03:16,400 --> 00:03:19,160 you'll call the base class's constructor. 54 00:03:19,160 --> 00:03:22,590 Lastly, you'll review your media type subclasses, and 55 00:03:22,590 --> 00:03:26,430 remove any title field definitions from those classes. 56 00:03:26,430 --> 00:03:30,382 You'll also remove from the constructors in those classes, 57 00:03:30,382 --> 00:03:33,042 any related field initialization code. 58 00:03:33,042 --> 00:03:35,280 And that's your first challenge. 59 00:03:35,280 --> 00:03:38,880 Don't worry if you aren't able to complete every part of this challenge. 60 00:03:38,880 --> 00:03:43,960 Complete as much of it as you can, and in the next video, I'll show you my solution. 61 00:03:43,960 --> 00:03:46,130 Good luck, and we'll see you in just a bit.