1 00:00:00,440 --> 00:00:01,480 Hello, everyone. 2 00:00:01,640 --> 00:00:03,400 Welcome to sequence 3. 3 00:00:03,560 --> 00:00:06,040 Now that we have looked at inheritance, 4 00:00:07,400 --> 00:00:10,400 self, and lookup, we'll examine "super." 5 00:00:10,840 --> 00:00:14,280 We'll start by focusing on sending a message 6 00:00:14,880 --> 00:00:17,840 in cases where "super" is the receiver. 7 00:00:18,360 --> 00:00:19,840 What is "super"? 8 00:00:20,120 --> 00:00:25,360 Take a few minutes to think of a definition covering these two points: 9 00:00:26,040 --> 00:00:28,200 What does "super" represent? 10 00:00:30,400 --> 00:00:32,200 How is a method looked up 11 00:00:32,360 --> 00:00:34,240 when a message is sent to "super"? 12 00:00:35,160 --> 00:00:36,560 The following principle 13 00:00:36,920 --> 00:00:39,200 is the same in both Pharo and Java. 14 00:00:39,560 --> 00:00:41,640 The example you see here 15 00:00:42,800 --> 00:00:46,240 is similar to the ones we saw in earlier sequences. 16 00:00:46,680 --> 00:00:49,360 "Super" is used in one method. 17 00:00:52,320 --> 00:00:55,600 Let's think about what happens. 18 00:00:56,440 --> 00:00:57,920 For A new bar, 19 00:00:59,560 --> 00:01:00,920 there's no problem. 20 00:01:01,080 --> 00:01:03,120 It's the same process as before. 21 00:01:03,320 --> 00:01:05,600 The method is run, 22 00:01:06,160 --> 00:01:08,080 and foo returns 10. 23 00:01:10,320 --> 00:01:13,440 In the case of B new bar, let's look at each step. 24 00:01:15,560 --> 00:01:17,960 We send the message "bar" 25 00:01:18,360 --> 00:01:20,720 to the B new object. 26 00:01:22,080 --> 00:01:24,840 The lookup algorithm seeks the method "bar" 27 00:01:25,160 --> 00:01:27,160 starting with the object class. 28 00:01:27,320 --> 00:01:29,800 It finds the method "bar." 29 00:01:30,880 --> 00:01:33,320 It is executed on the receiver. 30 00:01:34,000 --> 00:01:38,680 This "bar" method is the one executed on the receiver. 31 00:01:40,040 --> 00:01:43,440 Now I have to compute "super bar" 32 00:01:44,200 --> 00:01:46,640 and "self foo," and add them together. 33 00:01:46,880 --> 00:01:48,600 We can do "self foo," 34 00:01:48,760 --> 00:01:53,400 "self foo" sends the message "foo" to the object "self." 35 00:01:53,800 --> 00:01:55,600 "Self" is my B new. 36 00:01:56,640 --> 00:02:00,080 The only "foo" possible is the one in class A. 37 00:02:01,280 --> 00:02:03,160 So "self foo" returns 10. 38 00:02:04,840 --> 00:02:06,920 In "super bar," 39 00:02:08,000 --> 00:02:09,720 "super" is the receiver. 40 00:02:10,440 --> 00:02:12,920 But the lookup algorithm changes 41 00:02:13,120 --> 00:02:15,720 in the presence of the receiver "super." 42 00:02:16,680 --> 00:02:19,840 It will seek the method "bar" 43 00:02:20,320 --> 00:02:24,160 starting with the super class containing the keyword "super." 44 00:02:25,080 --> 00:02:29,200 "Super" is located in the "bar" method of class B. 45 00:02:29,640 --> 00:02:32,960 We'll be looking for method "bar" 46 00:02:33,120 --> 00:02:35,680 starting in B's superclass, which is A. 47 00:02:36,240 --> 00:02:40,720 So we find this method and that is the one executed. 48 00:02:41,000 --> 00:02:43,200 Foo is sent to self. 49 00:02:43,360 --> 00:02:46,480 "Self" is always the receiving object. 50 00:02:46,880 --> 00:02:47,880 It is B new. 51 00:02:48,120 --> 00:02:51,200 So foo is sent to the initial object. 52 00:02:51,560 --> 00:02:54,640 This is the "foo" that is executed, returning 10. 53 00:02:56,800 --> 00:02:58,400 10 + 10 = 20. 54 00:03:00,320 --> 00:03:02,480 In the case of C new bar, 55 00:03:02,920 --> 00:03:04,800 we note C new, here. 56 00:03:05,120 --> 00:03:09,120 It is an instance of class C. We send it the message "bar." 57 00:03:09,800 --> 00:03:14,320 "Bar" is sought in class C new. 58 00:03:14,640 --> 00:03:17,080 Not found. The class above is searched. 59 00:03:17,480 --> 00:03:18,640 It is there, in B. 60 00:03:18,800 --> 00:03:22,400 So the B new bar will be executed. 61 00:03:23,480 --> 00:03:27,960 This bar is the sum of two message-sendings. 62 00:03:28,640 --> 00:03:31,480 We can start with the second. 63 00:03:31,640 --> 00:03:34,640 We send the message "foo" to the object "self." 64 00:03:34,880 --> 00:03:39,200 Self is still my C New, here. I send the message "foo." 65 00:03:40,120 --> 00:03:42,760 The method is found, and 50 is returned. 66 00:03:44,280 --> 00:03:45,880 I put the value 50 there. 67 00:03:46,440 --> 00:03:50,960 Now we need the first part of the sum, "super bar." 68 00:03:51,240 --> 00:03:55,200 I send the message "bar" to "super," always the receiver. 69 00:03:55,360 --> 00:03:58,280 But the lookup algorithm changes. 70 00:03:59,000 --> 00:04:04,240 It looks for "bar" in the superclass for the class containing "super." 71 00:04:05,320 --> 00:04:08,720 That is, "bar" method in class B. 72 00:04:09,000 --> 00:04:13,240 The algorithm starts looking up "bar" in B's superclass, which is A. 73 00:04:13,880 --> 00:04:15,440 Bar is located here. 74 00:04:15,880 --> 00:04:17,360 This "bar" is executed. 75 00:04:17,640 --> 00:04:20,920 "Self" is still C new. Still this one. 76 00:04:21,080 --> 00:04:22,440 Always the receiver. 77 00:04:22,760 --> 00:04:24,440 We send it the message "foo." 78 00:04:24,600 --> 00:04:27,960 The result is 50, and I have 50 here. 79 00:04:28,280 --> 00:04:30,960 50 + 50 = 100. 80 00:04:31,680 --> 00:04:32,920 The next slide 81 00:04:33,360 --> 00:04:36,640 reviews the details of the lookup algorithm process, 82 00:04:36,800 --> 00:04:38,680 for further study. 83 00:04:39,680 --> 00:04:42,880 "Super" always refers to the receiver. 84 00:04:43,040 --> 00:04:45,120 It's exactly like "self," 85 00:04:45,320 --> 00:04:47,200 or "this" in Java. 86 00:04:47,480 --> 00:04:52,440 Likewise, "super" in Java is like "this," and always refers to the receiver. 87 00:04:53,160 --> 00:04:57,520 However, when a message is sent to "super," the lookup algorithm changes 88 00:04:57,680 --> 00:05:01,360 and begins searching for a method in the superclass 89 00:05:01,800 --> 00:05:05,680 of the class containing the method being executed. 90 00:05:06,040 --> 00:05:09,880 "Self" and "super" are therefore fundamentally different. 91 00:05:10,320 --> 00:05:13,640 "Self" is dynamic, while "super" is static. 92 00:05:13,880 --> 00:05:15,520 I'll explain that further. 93 00:05:17,080 --> 00:05:19,760 When "foo" is sent to "self," 94 00:05:20,360 --> 00:05:24,240 the developer has no idea which "foo" method will be executed. 95 00:05:24,880 --> 00:05:28,480 It could be the "foo" located in the same class, 96 00:05:29,120 --> 00:05:31,840 or a "foo" in an existing subclass, 97 00:05:32,200 --> 00:05:35,640 or a "foo" in a subclass created by another developer 98 00:05:35,920 --> 00:05:38,040 before the program is run. 99 00:05:38,720 --> 00:05:42,240 When the "bar" method developer writes "self foo," 100 00:05:42,480 --> 00:05:45,640 he has no idea which "foo" will be executed. 101 00:05:46,080 --> 00:05:48,160 That's a convenient feature. 102 00:05:48,480 --> 00:05:51,280 It means developers can create a new subclass 103 00:05:51,440 --> 00:05:54,320 to change the behavior of class A. 104 00:05:55,440 --> 00:05:58,120 Conversely, "super" is static. 105 00:05:58,880 --> 00:06:03,400 When the developer writes "super foo," 106 00:06:04,000 --> 00:06:06,920 he knows which "foo" method will be executed 107 00:06:07,080 --> 00:06:09,560 when the program is run. 108 00:06:09,840 --> 00:06:13,440 When he writes "super foo" here, he knows he is referring 109 00:06:14,280 --> 00:06:16,360 to "foo" in superclass A. 110 00:06:16,880 --> 00:06:18,680 Invariably. Super is static. 111 00:06:18,880 --> 00:06:21,640 We know how the program will be compiled. 112 00:06:22,080 --> 00:06:25,360 Unfortunately, certain books 113 00:06:25,720 --> 00:06:28,160 define "super" wrong. 114 00:06:28,320 --> 00:06:30,560 The definitions make no sense. 115 00:06:30,920 --> 00:06:33,800 Here is a definition we found in a book. 116 00:06:34,320 --> 00:06:37,480 It says that "super" looks for the method... 117 00:06:37,920 --> 00:06:42,280 In other words, "super" prompts the lookup algorithm 118 00:06:42,440 --> 00:06:47,240 to search for the method in the superclass of the receiver's class. 119 00:06:48,000 --> 00:06:50,480 The superclass of the receiver's class. 120 00:06:50,640 --> 00:06:54,360 Actually, this is wrong, as proved by the example shown. 121 00:06:54,800 --> 00:06:58,920 If you take aC as the receiver, its class is C. 122 00:06:59,720 --> 00:07:03,120 The receiver's superclass is therefore B. 123 00:07:04,040 --> 00:07:08,560 If I come down here and execute the command "super foo," 124 00:07:08,960 --> 00:07:12,960 if the definition given by the book applied, 125 00:07:13,640 --> 00:07:16,360 we would send the message "foo" 126 00:07:17,640 --> 00:07:20,520 to "super," and the "foo" method executed 127 00:07:20,680 --> 00:07:24,320 would be the one in the superclass of the receiver's class. 128 00:07:24,480 --> 00:07:27,480 In other words, this one. 129 00:07:27,840 --> 00:07:30,520 It would send "foo" to "super" 130 00:07:30,800 --> 00:07:34,480 over and over again, in an infinite loop. 131 00:07:35,680 --> 00:07:38,280 So the definition is wrong. 132 00:07:38,440 --> 00:07:43,760 In practice, this example functions perfectly. 133 00:07:44,240 --> 00:07:46,640 It's just that this definition is wrong. 134 00:07:47,280 --> 00:07:49,480 Here's what to remember: 135 00:07:50,040 --> 00:07:52,440 "Self" always represents the receiver. 136 00:07:52,680 --> 00:07:53,840 So does "super." 137 00:07:54,000 --> 00:07:58,040 It's the same in Java, with "this" and "super." 138 00:07:58,880 --> 00:08:01,160 However, "super" changes the lookup. 139 00:08:01,600 --> 00:08:05,000 It will start looking for a match 140 00:08:05,240 --> 00:08:07,320 in the superclass 141 00:08:07,560 --> 00:08:12,160 of the class containing the method that contains the keyword "super." 142 00:08:12,720 --> 00:08:15,240 "Self" sends are dynamic. 143 00:08:15,640 --> 00:08:20,240 The developer can use them to extend the behavior of an existing class, 144 00:08:20,440 --> 00:08:23,440 by creating a subclass that redefines the method.