1 00:00:00,259 --> 00:00:04,546 [MUSIC] 2 00:00:04,546 --> 00:00:08,970 In the first part of this course we wrote a class that models a point. 3 00:00:08,970 --> 00:00:11,550 It has x and y fields and 4 00:00:11,550 --> 00:00:16,050 even has a method that calculates how far it is from another point. 5 00:00:16,050 --> 00:00:21,030 The Treehouse defense game uses this class to identify locations on the map. 6 00:00:21,030 --> 00:00:22,780 But it could be used for a lot more than that. 7 00:00:24,070 --> 00:00:29,070 Lots of software applications use x and y coordinates to identify a location. 8 00:00:29,070 --> 00:00:32,500 Perhaps the next game we write will also need a point class. 9 00:00:32,500 --> 00:00:36,490 Software that displays charts and graphs also needs points. 10 00:00:36,490 --> 00:00:41,300 If we keep our implementation of the point class as general purpose as possible, 11 00:00:41,300 --> 00:00:43,880 we can reuse it in all those different applications. 12 00:00:45,050 --> 00:00:48,410 What if we need to add something to the point class that's specific 13 00:00:48,410 --> 00:00:50,070 to Treehouse defense? 14 00:00:50,070 --> 00:00:54,130 For example what if we wanted to make it impossible to create a point that 15 00:00:54,130 --> 00:00:55,710 isn't on the map? 16 00:00:55,710 --> 00:00:59,850 Making a change like that to the point class would make it unusable to 17 00:00:59,850 --> 00:01:02,470 all other applications that don't have maps. 18 00:01:02,470 --> 00:01:05,580 We don't want to add code to a general purpose class that we're 19 00:01:05,580 --> 00:01:08,400 fairly certain won't be useful to other users of the class. 20 00:01:09,810 --> 00:01:14,310 We also don't want to create a new class that's specific to Treehouse defense and 21 00:01:14,310 --> 00:01:17,280 rewrite everything we wrote for the point class. 22 00:01:17,280 --> 00:01:21,810 What we want is to create a new class that has our new functionality, but 23 00:01:21,810 --> 00:01:24,480 also reuses the point class. 24 00:01:24,480 --> 00:01:26,980 The principles of object-oriented programming 25 00:01:26,980 --> 00:01:31,250 provide lots of ways to reuse and extend existing classes. 26 00:01:31,250 --> 00:01:33,070 One of them is called inheritance. 27 00:01:34,350 --> 00:01:38,550 There are four core principles of object-oriented programming. 28 00:01:38,550 --> 00:01:43,300 Encapsulation, inheritance, polymorphism and abstraction. 29 00:01:43,300 --> 00:01:48,710 By saying that C# is an object-oriented programming language, we're really saying 30 00:01:48,710 --> 00:01:53,690 that C# has features built into the language to support these four principles. 31 00:01:54,910 --> 00:01:59,340 As you can see, inheritance is one of the four core principles. 32 00:01:59,340 --> 00:02:02,290 Don't worry about these other three principles right now. 33 00:02:02,290 --> 00:02:04,170 We'll discuss them later. 34 00:02:04,170 --> 00:02:08,549 The principle we want to focus on right now is inheritance. 35 00:02:08,549 --> 00:02:09,144 You see, 36 00:02:09,144 --> 00:02:15,020 many objects in the real world often share many of the same attributes and behaviors. 37 00:02:15,020 --> 00:02:20,960 A classic example is an animal, all animals have similar characteristics. 38 00:02:20,960 --> 00:02:25,792 For example, a defining characteristic of all animals is the ability to move. 39 00:02:25,792 --> 00:02:30,570 There are two types of animals, vertebrates and invertebrates. 40 00:02:30,570 --> 00:02:34,040 Vertebrates have backbones and invertebrates don't. 41 00:02:34,040 --> 00:02:36,640 But they're both still animals, so they can both move. 42 00:02:37,990 --> 00:02:40,400 Mammals and birds are types of vertebrates. 43 00:02:40,400 --> 00:02:43,050 By saying that a creature is a mammal we're 44 00:02:43,050 --> 00:02:46,690 also saying that it has a backbone and it can move. 45 00:02:46,690 --> 00:02:47,820 You might say, 46 00:02:47,820 --> 00:02:51,460 that it inherits these characteristics from the larger categories, 47 00:02:51,460 --> 00:02:55,690 or classifications, of vertebrates and animals of which it's a member. 48 00:02:56,810 --> 00:03:02,270 You can see now that the C# key word class is short for classification. 49 00:03:02,270 --> 00:03:06,550 C# classes define what it means to be in a particular classification. 50 00:03:07,550 --> 00:03:11,150 As we saw with the animal example, classes of animals 51 00:03:11,150 --> 00:03:16,720 can have more refined classifications within them, we call these subclasses. 52 00:03:16,720 --> 00:03:21,380 Subclasses inherit the attributes and behaviors of the more general classes. 53 00:03:22,840 --> 00:03:25,840 So if we wanted a new type of point that inherited 54 00:03:25,840 --> 00:03:28,110 all of the features of the point class, but 55 00:03:28,110 --> 00:03:33,990 was more specific to our application, we just need to create a subclass of point. 56 00:03:33,990 --> 00:03:35,470 Let's look at how we do that in code.