1 00:00:00,160 --> 00:00:00,200 "Learning Object-Oriented Programming and Design with TDD" 2 00:00:00,520 --> 00:00:00,560 "First look at Objects, Classes, Methods and messages" 3 00:00:00,880 --> 00:00:00,920 Stéphane Ducasse, research director, Inria. 4 00:00:01,640 --> 00:00:04,200 -In this lecture, I will do a survey of all the concepts 5 00:00:04,520 --> 00:00:06,520 that we will see in future lectures. 6 00:00:06,840 --> 00:00:12,840 So, we will look at what objects, classes, methods and messages are. 7 00:00:14,120 --> 00:00:15,080 So, what is an object? 8 00:00:15,400 --> 00:00:17,120 In fact, an object is a small living entity 9 00:00:17,440 --> 00:00:19,520 in the object-oriented world, 10 00:00:19,840 --> 00:00:22,760 and this entity is characterized by three properties, 11 00:00:23,080 --> 00:00:24,640 a state, a behavior and an identity. 12 00:00:24,960 --> 00:00:26,400 So, let us look at an example. 13 00:00:26,720 --> 00:00:28,320 If we take a Logo turtle... 14 00:00:29,400 --> 00:00:32,240 A Logo turtle is a small entity, an object. 15 00:00:32,560 --> 00:00:36,760 It has a state. For example, the pen, if its pen is up or down, 16 00:00:37,080 --> 00:00:40,320 the size of the pen, the color and the direction. 17 00:00:40,640 --> 00:00:42,680 And the turtle also has a behavior. 18 00:00:43,000 --> 00:00:45,800 This means that it knows how to draw lines on the floor, 19 00:00:46,120 --> 00:00:48,640 jump or hide itself. 20 00:00:48,960 --> 00:00:52,720 But also, these turtles have an identity, 21 00:00:53,040 --> 00:00:55,600 and the idea is that, when you look at the picture, 22 00:00:55,920 --> 00:00:59,000 you have two turtles, one spider and one turtle, 23 00:00:59,320 --> 00:01:02,320 and they are different entities, the spider is not the turtle. 24 00:01:03,640 --> 00:01:07,480 Now, in fact, in the object-oriented programming paradigm, 25 00:01:07,800 --> 00:01:08,960 everything is an object, 26 00:01:09,280 --> 00:01:13,120 so what you will see is that even a file that you have on your disk is an object. 27 00:01:13,440 --> 00:01:15,720 It is represented by a path and a content. 28 00:01:16,040 --> 00:01:17,440 That is the state of the object. 29 00:01:17,760 --> 00:01:18,520 The behavior is... 30 00:01:18,840 --> 00:01:22,480 For example, you can copy your file, or you can rename the file. 31 00:01:22,800 --> 00:01:26,200 And the identity is clearly that a file is different from another one. 32 00:01:26,520 --> 00:01:29,280 Else, you would have only one on your hard disk. 33 00:01:29,600 --> 00:01:31,120 Now, I would like to say something 34 00:01:31,440 --> 00:01:34,480 about the difference between identity and equality. 35 00:01:34,800 --> 00:01:38,640 This can be confusing, but here is something really simple. 36 00:01:38,960 --> 00:01:41,960 Imagine that you enter a pizzeria and you want to order a pizza. 37 00:01:42,280 --> 00:01:44,520 There is a guy eating a pizza, and you want the same. 38 00:01:44,840 --> 00:01:47,640 Identity would mean that you go and eat his own pizza, 39 00:01:47,960 --> 00:01:52,360 whereas equality means that you want to eat the same pizza. 40 00:01:52,680 --> 00:01:56,240 So, you see that identity is the pointer to the object, 41 00:01:56,560 --> 00:02:00,480 whereas equality is something that is similar to the object. 42 00:02:00,800 --> 00:02:03,120 Now, let us go back to the object. 43 00:02:03,440 --> 00:02:06,480 An object reacts to messages, and it will perform an action. 44 00:02:06,800 --> 00:02:09,400 So, what I show on the slide is that you have a turtle, 45 00:02:09,720 --> 00:02:11,800 and then, you send a message to the turtle 46 00:02:12,120 --> 00:02:13,560 to go to a certain location. 47 00:02:13,880 --> 00:02:16,240 Then, you send the message "go to: 100", 48 00:02:16,560 --> 00:02:19,760 and then, the turtle will go and leave a trace on the floor. 49 00:02:20,960 --> 00:02:21,960 Now, what we should see 50 00:02:22,280 --> 00:02:25,360 is that different objects can understand the same message. 51 00:02:25,680 --> 00:02:27,680 Each object can perform a different reaction, 52 00:02:28,000 --> 00:02:29,880 but this is still the same object. 53 00:02:30,200 --> 00:02:32,640 When we have "aWindow open", we will open a window. 54 00:02:32,960 --> 00:02:35,920 When we have "aFile open", we will get the content of the file. 55 00:02:36,240 --> 00:02:40,840 And if you have "aZip open", you will get the content of the Zip. 56 00:02:42,560 --> 00:02:43,600 Now, in the vocabulary, 57 00:02:43,920 --> 00:02:48,360 it is really important to understand what the message receiver is. 58 00:02:48,680 --> 00:02:49,440 The message receiver 59 00:02:49,760 --> 00:02:52,600 identifies the object that actually receives the message. 60 00:02:52,920 --> 00:02:55,960 For example, when I say "aTurtle jumpTo: aLocation", 61 00:02:56,280 --> 00:02:59,520 I send the message "jumpTo" to a turtle, and this is the receiver. 62 00:02:59,840 --> 00:03:03,760 And similarly, if I say "aWindow open", the receiver is a window, 63 00:03:04,080 --> 00:03:05,960 and it is the same for "aFile open". 64 00:03:06,640 --> 00:03:09,520 Now, what is a method? It is in fact a sequence of instructions, 65 00:03:09,840 --> 00:03:10,960 and we give it a name. 66 00:03:11,280 --> 00:03:14,360 So, let us take the example of crepes. 67 00:03:14,680 --> 00:03:15,680 To make crepes, 68 00:03:16,000 --> 00:03:20,920 we know that we need eggs, milk, flour and some sugar. 69 00:03:21,240 --> 00:03:23,680 This is a recipe, and a method is a recipe. 70 00:03:24,000 --> 00:03:25,880 It is a named sequence of instructions 71 00:03:26,200 --> 00:03:29,080 that the object will execute in response to a message. 72 00:03:30,560 --> 00:03:33,360 So, what is the difference between a message and a method? 73 00:03:33,680 --> 00:03:36,080 A message is what to do. It is a kind of order. 74 00:03:36,400 --> 00:03:38,520 "stef makeCrepes", "robert makeCrepes"... 75 00:03:38,840 --> 00:03:42,800 Those are orders, and a method is how to do it. 76 00:03:43,120 --> 00:03:45,160 It is a list of steps. 77 00:03:45,480 --> 00:03:50,160 So, the message is what to do, whereas the method is how to do it. 78 00:03:51,760 --> 00:03:54,720 It is important to understand that we can have one message 79 00:03:55,040 --> 00:03:57,200 and multiple methods with the same name. 80 00:03:57,520 --> 00:03:58,880 When I have "Window >> open", 81 00:03:59,200 --> 00:04:01,360 I will have a specific sequence to open a window. 82 00:04:01,680 --> 00:04:02,680 When I have "File >> open", 83 00:04:03,000 --> 00:04:05,160 I will have a specific sequence to open a file. 84 00:04:05,480 --> 00:04:09,200 And still, they have the same message, they understand the same message. 85 00:04:10,320 --> 00:04:13,520 Now, let us look at classes, because objects come from somewhere. 86 00:04:13,840 --> 00:04:17,520 Objects come from classes, and a class is like a factory of objects. 87 00:04:17,840 --> 00:04:21,880 A class acts as a mold for objects. 88 00:04:22,200 --> 00:04:24,280 If you check the picture on the slide, 89 00:04:24,600 --> 00:04:26,760 this is a bathtub-making system. 90 00:04:27,080 --> 00:04:30,120 You put some steel and you get a bathtub. 91 00:04:30,440 --> 00:04:33,880 What is important is that the class will create many instances, 92 00:04:34,200 --> 00:04:36,840 but each instance is still a separate entity. 93 00:04:37,160 --> 00:04:39,640 This means that you can destroy one bathtub, 94 00:04:39,960 --> 00:04:41,840 but you will not destroy the other ones. 95 00:04:42,160 --> 00:04:44,840 Each entity has its own identity. 96 00:04:45,160 --> 00:04:48,480 Still, they all are instances of a class. 97 00:04:48,800 --> 00:04:51,000 Now, if you look at it from another perspective, 98 00:04:51,320 --> 00:04:55,160 a class will define all the variables of its instances, 99 00:04:55,480 --> 00:04:59,600 and it will define the behavior that the instances should have. 100 00:05:01,720 --> 00:05:04,280 So, how do we create an object? 101 00:05:04,600 --> 00:05:07,120 In fact, we send the message "new" to a class. 102 00:05:07,440 --> 00:05:10,040 For example, here, I have a class turtle and I ask it: 103 00:05:10,360 --> 00:05:13,600 "I want to have a new instance, can you create one for me?" 104 00:05:13,920 --> 00:05:18,360 I will send the message "new", and I will get a new turtle. 105 00:05:18,680 --> 00:05:21,080 And that is the same for every object in the system. 106 00:05:21,400 --> 00:05:24,600 For a file, if I send the message "new", I will get a file. 107 00:05:25,960 --> 00:05:30,840 What is important to understand is that methods are dynamically looked up 108 00:05:31,160 --> 00:05:32,400 in the class of the receiver. 109 00:05:32,720 --> 00:05:37,760 So, here, I show three different kinds of classes and objects. 110 00:05:38,080 --> 00:05:39,880 I have a dog, instance of the class "dog", 111 00:05:40,200 --> 00:05:41,560 a cat, instance of the class "cat" 112 00:05:41,880 --> 00:05:45,760 and a fish, instance of the class "fish", and they can be afraid. 113 00:05:47,760 --> 00:05:50,320 But the dog will react differently from the cat. 114 00:05:50,640 --> 00:05:53,240 This means that when I send the message "angry" to a dog, 115 00:05:53,560 --> 00:05:55,280 I will look in the class "dog" 116 00:05:55,600 --> 00:05:58,520 to find the correct methods that will be executed. 117 00:05:58,840 --> 00:05:59,880 And this is the same. 118 00:06:00,200 --> 00:06:04,920 I have one message that is "angry", and I will send it to multiple instances, 119 00:06:05,240 --> 00:06:07,800 and they will look respectively in their class 120 00:06:08,120 --> 00:06:11,800 to find the correct behavior, the correct methods to be executed. 121 00:06:12,920 --> 00:06:15,880 So, with that, we have basically covered 122 00:06:16,200 --> 00:06:19,400 all the most important concepts in object-oriented programming. 123 00:06:19,720 --> 00:06:22,560 An object is a unique entity and an instance of a class. 124 00:06:22,880 --> 00:06:25,640 A class specifies the shape and the behavior 125 00:06:25,960 --> 00:06:27,480 of all its instances. 126 00:06:27,800 --> 00:06:31,800 An object receives messages 127 00:06:32,120 --> 00:06:34,960 and executes corresponding methods. 128 00:06:35,280 --> 00:06:36,200 A message is an order 129 00:06:36,520 --> 00:06:40,560 and a method is a sequence of instructions. 130 00:06:41,680 --> 00:06:44,680 And different objects can understand the same message 131 00:06:45,000 --> 00:06:46,960 but react differently. 132 00:06:47,280 --> 00:06:50,600 This is what we will see in future lectures.