1 00:00:01,160 --> 00:00:04,960 -Hello. In this lecture, we will look at what classes are. 2 00:00:05,280 --> 00:00:08,240 Basically, a class is a factory of objects. 3 00:00:08,560 --> 00:00:12,160 But more precisely, a class is a mold of objects. 4 00:00:12,480 --> 00:00:15,480 What you see on the slide is that we have this big machine 5 00:00:15,800 --> 00:00:16,960 that creates bathtubs. 6 00:00:17,280 --> 00:00:22,120 And what you see is that this machine will create many bathtubs. 7 00:00:22,440 --> 00:00:26,600 Probably thousands or hundreds of thousands of them. 8 00:00:26,920 --> 00:00:31,280 A class will also create hundreds of thousands of objects, 9 00:00:31,600 --> 00:00:33,720 and all these objects are still autonomous. 10 00:00:34,040 --> 00:00:35,880 This means that you can take an object, 11 00:00:36,200 --> 00:00:38,440 and this object can have specific properties, 12 00:00:38,760 --> 00:00:41,480 a state that is not shared by the other objects. 13 00:00:41,800 --> 00:00:45,960 But since they are all created by the same class, 14 00:00:46,280 --> 00:00:48,880 all these objects will share the same behavior. 15 00:00:50,800 --> 00:00:55,680 So, what we see is that in fact, a class defines all the variables 16 00:00:56,000 --> 00:00:59,000 and the characteristics that an object will have. 17 00:00:59,320 --> 00:01:01,880 And all its instances will have the same characteristics. 18 00:01:02,200 --> 00:01:03,800 And a class defines the behavior, 19 00:01:04,120 --> 00:01:06,880 which will be shared by all these objects. 20 00:01:07,200 --> 00:01:12,600 The idea is that the object should not define all the same methods. 21 00:01:12,920 --> 00:01:15,240 These methods are shared by all the instances. 22 00:01:15,560 --> 00:01:17,040 This way, we optimize space 23 00:01:17,360 --> 00:01:21,960 and we do not have to have thousands of similar methods in the system. 24 00:01:24,160 --> 00:01:26,920 A class factorizes object properties. 25 00:01:27,240 --> 00:01:32,520 For example, all rectangles will have a height and a width, 26 00:01:34,040 --> 00:01:38,120 but a given rectangle will have a value for the height and the width 27 00:01:38,440 --> 00:01:40,240 different from another rectangle. 28 00:01:40,560 --> 00:01:42,800 We will define the behavior once, 29 00:01:43,120 --> 00:01:47,320 and all the objects will be able to reuse this behavior. 30 00:01:47,640 --> 00:01:50,920 In fact, all the instances of the class will reuse this behavior. 31 00:01:51,240 --> 00:01:54,560 This is what I show with the definition of the method "top". 32 00:01:54,880 --> 00:01:58,320 All the rectangles will be able to answer 33 00:01:59,640 --> 00:02:02,040 and execute this method in reaction to a message. 34 00:02:03,600 --> 00:02:08,000 What I show in this UML diagram is that, for example, 35 00:02:09,320 --> 00:02:13,440 rectangles, here, are represented by two points, 36 00:02:13,760 --> 00:02:15,520 the origin and the corner. 37 00:02:15,840 --> 00:02:19,040 And they offer multiple methods to the system, 38 00:02:19,360 --> 00:02:21,520 like "area", "topCorner", etc. 39 00:02:21,840 --> 00:02:23,280 Now, what is really important 40 00:02:23,600 --> 00:02:24,840 is that each of the rectangles 41 00:02:25,160 --> 00:02:26,960 that are instances of the class "rectangle" 42 00:02:27,280 --> 00:02:28,200 has different properties. 43 00:02:28,520 --> 00:02:31,640 You can have a rectangle that is flat, one that looks like a square, 44 00:02:31,960 --> 00:02:33,760 or one that is really tall. 45 00:02:34,080 --> 00:02:40,320 They all have a width and a height, and they will share the same behavior, 46 00:02:40,640 --> 00:02:41,840 but they have different values, 47 00:02:42,160 --> 00:02:44,480 so each instance will have a specific value. 48 00:02:46,240 --> 00:02:48,880 This is what I said in this slide. 49 00:02:49,200 --> 00:02:53,200 The class defines the properties 50 00:02:53,520 --> 00:02:57,040 that all of its instances should hold, 51 00:02:57,360 --> 00:02:58,560 but not the actual value. 52 00:02:58,880 --> 00:03:01,680 Each instance will have its own specific value. 53 00:03:02,000 --> 00:03:03,840 And then, the class defines the behavior 54 00:03:04,160 --> 00:03:07,160 that is shared among all the instances of the class. 55 00:03:08,880 --> 00:03:12,120 If you look at the differences between classes and instances... 56 00:03:12,440 --> 00:03:15,200 An object is an instance of a class. 57 00:03:15,520 --> 00:03:17,920 It has been created by a class. 58 00:03:18,240 --> 00:03:20,880 A class will specify the behavior 59 00:03:21,200 --> 00:03:25,560 that all its instances will comply to or execute, 60 00:03:25,880 --> 00:03:29,200 and this behavior is shared among all the instances of a class. 61 00:03:29,520 --> 00:03:33,440 And a class will specify the structure of the instance, 62 00:03:33,760 --> 00:03:37,280 but not the value that the instance should hold. 63 00:03:39,600 --> 00:03:42,920 So, what we should see is that the instances of a class 64 00:03:43,240 --> 00:03:45,560 all share the same behavior. How does it work? 65 00:03:45,880 --> 00:03:49,600 In fact, when I send a message to one rectangle, 66 00:03:49,920 --> 00:03:52,880 I will look for the methods in the class of the rectangle, 67 00:03:53,200 --> 00:03:58,400 and I will find a method which will be applied on this object. 68 00:03:58,720 --> 00:04:01,120 That is why, if I send a message to another rectangle, 69 00:04:01,440 --> 00:04:05,040 I will find the same method and apply it to this other rectangle. 70 00:04:05,360 --> 00:04:07,880 This way, I share the behavior. 71 00:04:08,200 --> 00:04:13,080 So, all the instances of a class will share the same behavior. 72 00:04:15,160 --> 00:04:19,560 What we should see is that methods are looked up dynamically. 73 00:04:19,880 --> 00:04:24,560 This means that I will look for a method in the class of the object. 74 00:04:24,880 --> 00:04:28,840 Here is an example with dogs, cats and fish. 75 00:04:29,160 --> 00:04:33,000 When I send the message "angry" to a dog, 76 00:04:33,320 --> 00:04:34,720 I will look in the class "dog", 77 00:04:35,040 --> 00:04:37,800 find the definition of the method and execute it. 78 00:04:38,120 --> 00:04:41,400 And this behavior is different from the one in the class "cat". 79 00:04:41,720 --> 00:04:45,240 But all the dogs will execute the same method. 80 00:04:47,600 --> 00:04:50,000 And here again, this is the same message 81 00:04:50,320 --> 00:04:52,960 that I will send to different instances of different classes 82 00:04:53,280 --> 00:04:55,680 and that will execute different methods. 83 00:04:56,000 --> 00:04:59,200 So, when I send the message "angry" to an instance of the class "cat", 84 00:04:59,520 --> 00:05:01,440 I will not execute the same method 85 00:05:01,760 --> 00:05:06,000 as when I send the message "angry" to an instance of the class "dog". 86 00:05:07,400 --> 00:05:11,600 So, what you should know is that any object is an instance of a class. 87 00:05:11,920 --> 00:05:13,360 A class is a factory of objects. 88 00:05:13,680 --> 00:05:20,200 It describes the state of an object, of every of its instances. 89 00:05:20,520 --> 00:05:24,920 And it defines the behavior that all its instances will share.