Inheritance in Code5:27 with Jeremy McLain
Let's use inheritance to make a subclass of the Point class.
Let's create a subclass of the point class to do that we need to create a new class, 0:00 let's call it map location. 0:06 So we'll create a new file called MapLocation.cs in here 0:08 we'll create a new class called map location. 0:12 We specify that the map location class is a subclass of the point class by 0:25 typing : Point after the name of the class here, and there you go. 0:30 We've just subclassed the point class. 0:35 We need to do one more thing to make this class usable. 0:38 When a map location object is created, we're creating a point object too. 0:42 They aren't two distinct objects, it's just that a map location is also a point. 0:47 In the same way that when a mammal is born, a vertebrate and 0:52 an animal are also born because a mammal is both a vertebrate and an animal. 0:56 Remember that the point object requires that we pass in both x and 1:02 y coordinates to the constructor in order for it to be created. 1:06 So we still have to satisfy those requirements 1:10 because a map location is just another type of point. 1:13 When a map location object is created it first needs to call the point class 1:17 constructor and pass it what it needs. 1:21 So the map location class will also need a constructor that takes the x and 1:24 y coordinates, so that it can pass them to the point class constructor. 1:28 Again, we name the constructor the same as the class name. 1:33 In this case, 1:39 it will take the same parameters as the constructor of the point class. 1:40 Now that we have a map location constructor, 1:48 we need to tell it to call the point class constructor and pass these parameters. 1:50 We do that by typing : base here and list the parameters. 1:55 We use the base keyword here because the point class is MapLocation's base class. 1:59 You'll hear people refer to the base class as the parent class or the super class and 2:05 you'll hear people refer to the sub class as the child class or derived class. 2:10 We're calling the base classes constructor here and passing it the values it needs. 2:17 So now, any map location objects we create will also be point objects, 2:22 let's go to main to see what this means. 2:28 Instead of using the point class here, 2:30 let's change it to use the map location class. 2:33 To avoid any confusion I'll rename this variable to x. 2:44 Even though x is now a map location, this line here will still work. 2:52 The map location class doesn't have a distance to method directly but 2:58 it inherits it from the point class or 3:03 you could say that x is both a point and a map location. 3:06 So it has all of the methods of both classes. 3:10 We can even assign map location to a point. 3:13 We can also assign it to a point at the same time we create it. 3:19 We can even pass MapLocation objects into methods that are expecting a point. 3:23 Remember the maps OnMapMethod, that takes a point as a parameter? 3:29 Let's pass it a MapLocation instead. 3:33 Let's compile just to make sure this works. 3:48 See, no compiler errors. 3:53 These all works because map location is a subclass of point. 3:56 We can say that the point and map location classes have an is a relationship, 4:01 in that map location is a point. 4:07 There's even a way in C# to see if an object is a certain type. 4:10 The is operator returns true if a variable is of a given type 4:14 this is called a type check. 4:20 Let's print out the result of a few type checks. 4:22 Let's first check to see if x is a map location. 4:26 Let's also check to see if x is a point. 4:35 Finally let's create a new point object and see if it's of type map location. 4:44 What do you think the result of this expression will be? 4:53 Let's run it and see. 5:03 As you can see, x is both the map location and a point but 5:12 point is not a map location. 5:17 That would be like saying that an animal is a vertebrate, 5:20 when the only thing we know is that it's an animal. 5:24
You need to sign up for Treehouse in order to download course files.Sign up