BOSS 7 3!   %ByteString@Inheritance, Self and SuperL!!H ," %" %"%" %"%" %"" " 4 f##;# | e#F $ H5$ 9 # #C#D#!: s$L#i  +%Z@1%d7t'%-O$<5i3 *D *9-'Objectsubclass:instanceVariableNames:classVariableNames:poolDictionaries:category:name M206 - Student classes 8name "8&initializestudent-authoredCompiledMethodRelease 4.1bytesmclasssourceCode>BF6:@JA Grandparentname:CD*RVeE!initialize "sets the initial value of name to reflect the class, returns the receiver" self name: 'A Grandparent'*.2NZ^name@e*name "returns the instance variable name" ^name*f2Njn`X*name: aString "sets the instance variable name to aString, returns the receiver" name := aString*V2NvztestUnderstandinggrandparentCD*VeE!testUnderstanding "sets the value of name to reflect the class in which the method was called" self name: 'grandparent'*2NclassSideInstanceVariableNames:*superclassmethodDictformatsubclassesinstanceVariablesorganizationnameclassPoolsharedPools8'newByteArray@DAenew "returns a new instance and initializes " ^super new initialize2NdeferSend:MessageSendRelease 4.1selectorargsreceivercomment: *instVarCommentDict:"*&classVarCommentDict:.2*6orderedCategories:2B>F*J RV^b.jn2v>z~*H 5!!!9, /"m "o1 /":7"u c57.(b.  %|=}=#4x&M206 - Student classesxX<  p<A ParentCDeE!initialize "sets the initial value of name to reflect the class, returns the receiver" self name: 'A Parent'|"&parentCD.eE!testUnderstanding "sets the value of name to reflect the class in which the method was called" self name: 'parent'26\TX>8BlJ8N|V8Zbf8jN! 0!!?!2' ""32f"n, 1#> " < "7#"t' [)A2 u8/7 P %6>q!M206 - Student classesX@  p@A ChildCDeE!initialize "sets the initial value of name to reflect the class, returns the receiver" self name: 'A Child'|"&checkUnderstandingCD@echeckUnderstanding "sends message testUnderstanding to self or super as a check on understanding of how they work and identify the class in which testuderstandingis found" ^super testUnderstanding.26childCD>eE!testUnderstanding "sets the value of name to reflect the class in which the method was called" self name: 'child'BF\TXN8RlZ8^|f8jrv8z@ :!+"! !E1 3" 2y"s7 9" )"y  <:-n3 *:I /9+M206 - Student classesdXD  pDA GrandchildCDeE!initialize "sets the initial value of name to reflect the class, returns the receiver" self name: 'A Grandchild'|"&grandchildCD.eE!testUnderstanding "sets the value of name to reflect the class in which the method was called" self name: 'grandchild'26\TX>8BlJ8N|V8Zbf8jFHB4! ! ?7!!Y!-!s! =!$! !!N*!! h9! }& #R# "]X; #B=# $ #;23#"r" 7 ## ## "/"/ \# "+""":= "&$O #V#?#p'# % *;$*$ #r.#(4 B$&$ :###e #2 >%1O#,Ml(;'&( w,B( *=('  ( 72('9Q\8( d 'v,"( G'(' $v!C,(+'S*0( !0* 7+1W+m9,0.--,7'-f4 U+=2 ' |,!; ! ##!=2Q6H.?#o:q(# J3 ^0k"#<2) F, ,':' 6 +( (!(}0(((`))(B ' (( 5( '; U?0!(<'6o'z8"( ' ^. ' ,"T :;;' 1 '8!/ $( <( ;>y>(7' m'+'T':% %$("(U4 >' /(''I7' o ((  (<"' X*'' r9' ' '00" Y'(: )(0',' o';gJ13?(B2d(#3\($ ).~8) Q% 77v!`%F ?z'+-6U<?;-#o )<J Y$0d/2 +cU  K&~>d"LwBookNodedependentscollectionlimitcollectionSizecontentflagsinterfacelabellocalsnode0specvisioninclusionsname  "&*.26:;>newWithAll:content:flags:interface:label:locals:node0:spec:vision:name:inclusions:LwSectionNodevision "&*.NJ3RnewWithAll:content:flags:interface:label:locals:node0:spec:vision:LwPageNode "&*.^/bnewWithAll:content:flags:interface:label:locals:node0:spec:IdentityDictionarytallyvalueArrayvzr @~withKeysAndValues:alignmenttextTextstringruns GapStringstringgapStartgapSizeThis learning book has been constructed to give an addition insight into how inheritance works and the use of super and self. It is based on the discussions in the Tutor conferences. It seems simple but there are many lessons when one thinks about it! It is worth first looking in the Glossary at super and self to remind oneself of the definitions. Then go to the Section called Inheritance, Self and Super thinks about it! It is worth first looking in the Glossary at super and self to remind oneself of the definitions Then go to the Section called Inheritance, Self and Super This structure exercises ones understanding of Inheritance, Super and Self and 1. Before you start look in the Glossary at the definitions of super and self 2. There are four new classes created as successive subclasses under Object Object Grandparent Parent Child Grandchild 3. Grandparent declares the only extra instance variable called name and has two instance methods name: and name which set and return the value of name. 4. Grandparent has the only Class Method new which initializes name (why is one enough for all of these?) new ^super new initialize 5. Grandparent has an instance method initialize initialize self name: 'A Grandparent' 6. Similarly Parent has initialize self name: 'A Parent' and so on down the tree to 'A Grandchild'. 7. One should now check in the workspace by tests such as aChild := Child new. and inspecting Child that all the initialization works. 8. In what class does the new which has been used above sit and from what is it inherited. 9. Which initialize has been used and within it what does self refer to in each case, where has name: come from and which name has been set? Test your conclusions. 10. Now look at the instance method in each of the classes called testUnderstanding which is of the form testUnderstanding self name: 'grandparent' What do they do? 11. Now look at the single instance method checkUnderstanding in Child checkUnderstanding ^super testUnderstanding What will this do? What does super refer to? 12. Evaluate in the workspace aGrandparent := Grandparent new. aParent := Parent new. aChild := Child new. aGrandchild := Grandchild new. aGrandchild checkUnderstanding. 13. Write down what you expect to find and then Inspect, in particular, aGrandchild and aParent and explain name in each case 14. Remove the instance method testUnderstanding from Parent and repeat. Why is the result different? 15. Change checkUnderstanding to use self instead of super and repeat - why is this different? 16. Replace the instance method testUnderstanding in Parent in case one wants to repeat the exercise (or make ure you do not save the LB). 17. There are a number of extra games which can be played with the 4 levels to ensure that one has understood fully inheritance and how it is overruled and modified by super and self - try some out. 18. Go back to the definition of super in the Glossary - does it give the whole story or should it be modified? DISCUSSION The explanation of the difference between self and super in checkUnderstanding is: In both cases we work up the inheritance tree and the first (and only) case of 'checkUnderstanding' is in Child so sending 'checkUnderstanding' to an instance of Grandchild in the first case results in us having to evaluate 'super testUnderstanding' in Child as part of the process of executing checkUnderstanding. Super causes the search to start in the immediate ancestor of the class of the method that contains the word super. Here that class is Child (because 'super testUnderstanding' appears in Son's 'checkUnderstanding' method) and so the search starts in Parent for testUnderstanding where the method exists. If it is then removed as suggested in step 14. the method will be found in Grandparent. In the last case we have changed super to self in testUnderstanding. Now when one sends 'checkUnderstanding' to an instance of GrandChild it is the Grandchild's version of 'testUnderstanding' that gets used despite the fact that 'checkUnderstanding' is implemented in Child. This is because self always refers to the receiver so the self in 'self testUnderstanding' refers to the receiver. In this case the receiver is an instance of Grandchild and so the search for 'testUnderstanding' starts in Grandchild where the method exists. s@RunArrayrunsvaluescacheRuncacheRunStartsize12Associationkeyvalue colorColorValueredgreenblue ""&*.2OpenWordNoteswindowSpecfjn6:>BDictionaryvN@RfileNamecontents.htmZ^historyIndexfhistory^04rnvbjzV4~LwipGlossaryGlossaryfjJBFLwisGeneralNotesPoolDictionarydefiningClassv @VariableBindingvalue aGrandchildaGrandparentxaFatheraParentaChilddhaBagcontents#test V4aBagtom&adam.ted6fred> *2:BV4FJ"NsubGroupVZaGrandfatherbR^f4jVZnBThe following exercises are designed to give insight into Inheritance and the operation of Super and Self. The Class Browser here is the full version with all the basic system classes as well as those added by the OU visible. You may need to use Find more than you need to with the cut down versions in other Learning Books. 1. You will find it easiest to Detach this page whilst you are using the Class Browser and Workspace. 2. There are four new classes created as successive subclasses under Object Object Grandparent Parent Child Grandchild 3. Grandparent declares the only extra instance variable called name and has two instance methods name: and name which set and return the value of name. 4. Grandparent has the only Class Method new which initializes name (why is one enough for all of these?) new ^super new initialize 5. Grandparent has an instance method initialize initialize self name: 'A Grandparent' 6. Similarly Parent has initialize self name: 'A Parent' and so on down the tree to 'A Grandchild'. 7. One should now check in the workspace by tests such as aChild := Child new. and inspecting Child that all the initialization works. 8. In what class does the new which has been used above sit and from what is it inherited. 9. Which initialize has been used and within it what does self refer to in each case, where has name: come from and which name has been set? Test your conclusions. 10. Now look at the instance method in each of the classes called testUnderstanding which is of the form testUnderstanding self name: 'grandparent' What do they do? 11. Now look at the single instance method checkUnderstanding in Child checkUnderstanding ^super testUnderstanding What will this do? What does super refer to? 12. Evaluate in the workspace aGrandparent := Grandparent new. aParent := Parent new. aChild := Child new. aGrandchild := Grandchild new. aGrandchild checkUnderstanding. 13. Write down what you expect to find and then Inspect, in particular, aGrandchild and aParent and explain name in each case 14. Remove the instance method testUnderstanding from Parent and repeat. Why is the result different? 15. Change checkUnderstanding to use self instead of super and repeat - why is this different? 16. Replace the instance method testUnderstanding in Parent in case one wants to repeat the exercise (or make ure you do not save the LB). 17. There are a number of extra games which can be played with the 4 levels to ensure that one has understood fully inheritance and how it is overruled and modified by super and self - try some out. 18. Go back to the definition of super in the Glossary - does it give the whole story or should it be modified? d with the 4 levels to ensure that one has understood fully inheritance and how it is overruled and modified by super and self - try some out. 18. Go back to the definition of super in the Glossary - does it give the whole story or should it be modified? DISCUSSION The explanation of the difference between self and super in checkUnderstanding is: In both cases we work up the inheritance tree and the first (and only) case of 'checkUnderstanding' is in Child so sending 'checkUnderstanding' to an instance of Grandchild in the first case results in us having to evaluate 'super testUnderstanding' in Child as part of the process of executing checkUnderstanding. Super causes the search to start in the immediate ancestor of the class of the method that contains the word super. Here that class is Child (because 'super testUnderstanding' appears in Son's 'checkUnderstanding' method) and so the search starts in Parent for testUnderstanding where the method exists. If it is then removed as suggested in step 14. the method will be found in Grandparent. In the last case we have changed super to self in testUnderstanding. Now when one sends 'checkUnderstanding' to an instance of GrandChild it is the Grandchild's version of 'testUnderstanding' that gets used despite the fact that 'checkUnderstanding' is implemented in Child. This is because self always refers to the receiver so the self in 'self testUnderstanding' refers to the receiver. In this case the receiver is an instance of Grandchild and so the search for 'testUnderstanding' starts in Grandchild where the method exists. z+- W  "w ~Exercisesfjv:BinstanceClassRadioButtoninstanceRadioButtonselectedMethodStringselectedVariableStringselectedClassStringOULwipClassBrowser6Full Class BrowserfjBmessageAnswerObjectshowCheckBoxsourceTextModelaGrandparent := Grandparent new. aParent := Parent new. aChild := Child new. aGrandchild := Grandchild new. aGrandchild checkUnderstanding. ?resultTextModel LwipWorkspaceWorkspacefj"BDISCUSIOON The explanation of the difference between self and super in checkUnderstanding is: In both cases we work up the inheritance treeION The explanation of the difference between self and super in checkUnderstanding is: In both cases we work up the inheritance tree and the first (and only) case of 'checkUnderstanding' is in Child so sending 'checkUnderstanding' to an instance of Grandchild in the first case results in us having to evaluate 'super testUnderstanding' in Child as part of the process of executing checkUnderstanding. Super causes the search to start in the immediate ancestor of the class of the method that contains the word super. Here that class is Child (because 'super testUnderstanding' appears in Son's 'checkUnderstanding' method) and so the search starts in Parent for testUnderstanding where the method exists. If it is then removed as suggested in step 14. the method will be found in Grandparent. In the last case we have changed super to self in testUnderstanding. Now when one sends 'checkUnderstanding' to an instance of GrandChild it is the Grandchild's version of 'testUnderstanding' that gets used despite the fact that 'checkUnderstanding' is implemented in Child. This is because self always refers to the receiver so the self in 'self testUnderstanding' refers to the receiver. In this case the receiver is an instance of Grandchild and so the search for 'testUnderstanding' starts in Grandchild where the method exists. .K6:2>BFDiscussionfj*J:NB&RInheritance, Self and Super4^VZVZbBrfopenCompletenrV4vOULwinSystem4OUVisiondependentsdefinedincludedexcludedinitFromArray:Setv@GrandparentParentChildGrandchild4OUVisionSelections OUVisionSelectionclassNamerestrictionCodeselectorListmetaSelectorListvariableListmetaVariableList*OUVisionFilter OUVisionRecordnamerestrictionCode*"& 4**26 4: 4B 4B.>FJ*RV 4Z*bf 4j 4B 4B^nrvR~ 4b 4 4B 4BR 4b 4 4B 4BR 4b 4 4B 4BNz44B4BFjz~B