6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 14.2. 12 An initial class hierarchy On the other hand, if the question is asked by another professor, PERSON→ Why are you asking me动 why does this to then the arrogant-prof will respond by saying" why a paper on QvESTION 4 LANSWER Slide 14.2.13 An initial class hierarch The reason for introducing this behavior is to provide an xample of a class method, in which the action of the method depends on the object that initiated it. In particular, to incorporate this behavior, the arrogant-profs answer method (akpx。f命容ox-1 PROEESSOR? will need to do something different depending on what kind of object ask the question in the first place: if it was a student then PRoFESSOR STUDENT we want to respond one way, whereas if it was a professor, we want to respond a different way low do we do this? In essence, we need a way of tagging"our ARROGAN下平 objects, to identify their type. Within an object-oriented system, we choose to do this by adding to our classes a" predicate ANSWER method, which responds to the question of whether the instance is of that specified type. Thus, we can ask an object if they are a student, or a professor. (Actually, if you think about this, you will realize that we need to be careful in how we do this, since asking a professor if he is a student could lead to an error if the fact that the professor object does not have a student? method is not handled properly. We will see a particular way of dealing with this issue later.) Lessons from our simple class hierarchy Slide 14.2. 14 tagging of instances So what are the lessons we can take away from this simple specifying class hierarchies and ensuring that instances design exercise? Well, we can see that as we design a system for creating superclass instances supporting the creation of object-oriented systems, we need inheriting of methods from class hierarchies delegation of methods to other instances within a class methods for dealing with · tagging of instances specifying class hierarchies and ensuring that instances create superclass instances inheriting of methods from class hierarchies delegation of methods to other instances within a class hierarchy We will return to these issues in detail in later parts of the lectures6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 14.2.12 On the other hand, if the question is asked by another professor, then the arrogant-prof will respond by saying “why are you asking me about … I thought you published a paper on that topic” Slide 14.2.13 The reason for introducing this behavior is to provide an example of a class method, in which the action of the method depends on the object that initiated it. In particular, to incorporate this behavior, the arrogant-prof’s answer method will need to do something different depending on what kind of object ask the question in the first place: if it was a student then we want to respond one way, whereas if it was a professor, we want to respond a different way. How do we do this? In essence, we need a way of “tagging” our objects, to identify their type. Within an object-oriented system, we choose to do this by adding to our classes a “predicate” method, which responds to the question of whether the instance is of that specified type. Thus, we can ask an object if they are a student, or a professor. (Actually, if you think about this, you will realize that we need to be careful in how we do this, since asking a professor if he is a student could lead to an error if the fact that the professor object does not have a student? method is not handled properly. We will see a particular way of dealing with this issue later.) Slide 14.2.14 So what are the lessons we can take away from this simple design exercise? Well, we can see that as we design a system for supporting the creation of object-oriented systems, we need methods for dealing with: • tagging of instances • specifying class hierarchies and ensuring that instances create superclass instances • inheriting of methods from class hierarchies • delegation of methods to other instances within a class hierarchy We will return to these issues in detail in later parts of the lectures