1 00:00:00,580 --> 00:00:06,380 Let's create a subclass of the point class to do that we need to create a new class, 2 00:00:06,380 --> 00:00:08,410 let's call it map location. 3 00:00:08,410 --> 00:00:12,502 So we'll create a new file called MapLocation.cs in here 4 00:00:12,502 --> 00:00:15,856 we'll create a new class called map location. 5 00:00:25,668 --> 00:00:30,554 We specify that the map location class is a subclass of the point class by 6 00:00:30,554 --> 00:00:35,810 typing : Point after the name of the class here, and there you go. 7 00:00:35,810 --> 00:00:38,850 We've just subclassed the point class. 8 00:00:38,850 --> 00:00:42,040 We need to do one more thing to make this class usable. 9 00:00:42,040 --> 00:00:47,100 When a map location object is created, we're creating a point object too. 10 00:00:47,100 --> 00:00:52,880 They aren't two distinct objects, it's just that a map location is also a point. 11 00:00:52,880 --> 00:00:56,320 In the same way that when a mammal is born, a vertebrate and 12 00:00:56,320 --> 00:01:02,930 an animal are also born because a mammal is both a vertebrate and an animal. 13 00:01:02,930 --> 00:01:06,690 Remember that the point object requires that we pass in both x and 14 00:01:06,690 --> 00:01:10,620 y coordinates to the constructor in order for it to be created. 15 00:01:10,620 --> 00:01:13,720 So we still have to satisfy those requirements 16 00:01:13,720 --> 00:01:16,680 because a map location is just another type of point. 17 00:01:17,880 --> 00:01:21,860 When a map location object is created it first needs to call the point class 18 00:01:21,860 --> 00:01:24,890 constructor and pass it what it needs. 19 00:01:24,890 --> 00:01:28,480 So the map location class will also need a constructor that takes the x and 20 00:01:28,480 --> 00:01:33,750 y coordinates, so that it can pass them to the point class constructor. 21 00:01:33,750 --> 00:01:36,850 Again, we name the constructor the same as the class name. 22 00:01:39,620 --> 00:01:40,424 In this case, 23 00:01:40,424 --> 00:01:44,445 it will take the same parameters as the constructor of the point class. 24 00:01:48,155 --> 00:01:50,566 Now that we have a map location constructor, 25 00:01:50,566 --> 00:01:55,520 we need to tell it to call the point class constructor and pass these parameters. 26 00:01:55,520 --> 00:01:59,510 We do that by typing : base here and list the parameters. 27 00:01:59,510 --> 00:02:04,400 We use the base keyword here because the point class is MapLocation's base class. 28 00:02:05,580 --> 00:02:10,950 You'll hear people refer to the base class as the parent class or the super class and 29 00:02:10,950 --> 00:02:15,410 you'll hear people refer to the sub class as the child class or derived class. 30 00:02:17,210 --> 00:02:22,950 We're calling the base classes constructor here and passing it the values it needs. 31 00:02:22,950 --> 00:02:28,160 So now, any map location objects we create will also be point objects, 32 00:02:28,160 --> 00:02:30,800 let's go to main to see what this means. 33 00:02:30,800 --> 00:02:33,275 Instead of using the point class here, 34 00:02:33,275 --> 00:02:36,277 let's change it to use the map location class. 35 00:02:44,210 --> 00:02:47,917 To avoid any confusion I'll rename this variable to x. 36 00:02:52,686 --> 00:02:57,780 Even though x is now a map location, this line here will still work. 37 00:02:58,990 --> 00:03:03,630 The map location class doesn't have a distance to method directly but 38 00:03:03,630 --> 00:03:06,380 it inherits it from the point class or 39 00:03:06,380 --> 00:03:10,020 you could say that x is both a point and a map location. 40 00:03:10,020 --> 00:03:13,331 So it has all of the methods of both classes. 41 00:03:13,331 --> 00:03:16,057 We can even assign map location to a point. 42 00:03:19,521 --> 00:03:22,540 We can also assign it to a point at the same time we create it. 43 00:03:23,760 --> 00:03:29,500 We can even pass MapLocation objects into methods that are expecting a point. 44 00:03:29,500 --> 00:03:33,730 Remember the maps OnMapMethod, that takes a point as a parameter? 45 00:03:33,730 --> 00:03:36,414 Let's pass it a MapLocation instead. 46 00:03:48,149 --> 00:03:50,690 Let's compile just to make sure this works. 47 00:03:53,590 --> 00:03:56,050 See, no compiler errors. 48 00:03:56,050 --> 00:04:01,310 These all works because map location is a subclass of point. 49 00:04:01,310 --> 00:04:07,460 We can say that the point and map location classes have an is a relationship, 50 00:04:07,460 --> 00:04:10,550 in that map location is a point. 51 00:04:10,550 --> 00:04:14,930 There's even a way in C# to see if an object is a certain type. 52 00:04:14,930 --> 00:04:20,190 The is operator returns true if a variable is of a given type 53 00:04:20,190 --> 00:04:21,610 this is called a type check. 54 00:04:22,700 --> 00:04:24,990 Let's print out the result of a few type checks. 55 00:04:26,560 --> 00:04:29,660 Let's first check to see if x is a map location. 56 00:04:35,660 --> 00:04:38,175 Let's also check to see if x is a point. 57 00:04:44,996 --> 00:04:49,646 Finally let's create a new point object and see if it's of type map location. 58 00:04:53,733 --> 00:04:55,957 What do you think the result of this expression will be? 59 00:05:03,929 --> 00:05:05,703 Let's run it and see. 60 00:05:12,157 --> 00:05:17,046 As you can see, x is both the map location and a point but 61 00:05:17,046 --> 00:05:19,610 point is not a map location. 62 00:05:20,700 --> 00:05:24,150 That would be like saying that an animal is a vertebrate, 63 00:05:24,150 --> 00:05:26,530 when the only thing we know is that it's an animal.