View this PageEdit this PageAttachments to this PageHistory of this PageHomeRecent ChangesSearch the SwikiHelp Guide
Hotspots: Admin Pages | Turn-in Site |
Current Links: Cases Final Project Summer 2007

Fall2001 Midterm Review: OO Theory

Link back to Fall 2001 Midterm Review

1. A class is a blueprint for building a data model. An instance is a working model of the blueprint.
2. A class method is the method that is called when the message is sent to the class. A instance method is the method that is called when the message is sent to the instance of the class. Example

r := Rule new: 'hello' "new is a message sent to the class Rule telling it to create an instance of itself with 'hello' as a parameter"
r evaluate: 'whoops'. "evaluate is a message sent to the instance variable r. '

3. We didn't cover this in class although I think it would be very useful in some cases in P2(or the redesign of it atleast)
Jared Parsons

Man Jared your answer everything right before I do. However I have a pretty good guess as far as 3 goes, could a TA maybe verifiy this
3. Class variables are held by the class and not the instance of that class, so the the class can use this data while executing class methods. Instance variables are obviously the data that the instance holds.
Robert Schierholz

You've answered a couple pages while I was in the middle of edditing them. But I think you're right about class variables.
Jared Parsons
No, Robert. Class variables are not analagous to class methods, strange as it may seem. It's also considered bad design form to use them unless you must – and you rarely must. (I did cover them in class, Jared.) Mark Guzdial
I thought you said in class that we wouldn't be covering them because they were beyond the scope of this class. Hmm, maybe I was a little too sleepy that dya. Jared Parsons

3. The difference between a class variable and an instance variable is the scope of the variable. A class variable is available to all instances of that class. Class variables may be useful for information that should be available and accessible by all instances of the class, such as a count of cakes that is increment with each new cake (bad example). I agree with Guzdial on his comment on using class variables as being a bad design (except in very unique circumstances). I would rather use a separate class or a method that performs the function (perhaps counting instances). An instance variable is completely private and unique to an instance of a class or it's subclass. The only way to access instance variables is if there are accessor methods or other methods that return the value from them.
Christopher Henke
Jared, you're right that I did say that they weren't going to be covered in the course (is the comment about "scope" a pun? :-), but I did give the basic definition that Christopher just did. (Or so I recall. Sometimes, my last seven years of teaching 2390/2340 run together on me...:-) Mark Guzdial

A puzzle for advanced study: There actually aren't any "class" methods – that's just a distinction that the Browser makes for you. All methods are instance methods, and since all objects in Squeak are an instance of some class, "class" methods are just method that your class (as an instance) understands. But in what class are they defined? For example, if Box the class understands "new" the class method, but that's just an instance method, in what class is Box's "new" defined? Mark Guzdial

A class is an instance of MetaClass (I believe). So, the instance of MetaClass, which has the same name as the class name, has the methods. Since an instantiation of the class is not a subclass of MetaClass or the class itself, the methods are not inherited by the instantiation of a the class and are private to the instantiation of the class MetaClass that bears the name of the class and all subclasses of the class. This is the main reason why "class" methods can NOT be called by an instance of the class. These methods are only available to a MetaClass that represents the class or its subclass.

Confused? You're not alone...
Christopher Henke

So if there aren't any class methods, they are actually instance methods of MetaClass, then where do Class variables reside. I am assuming that they are not in the instance which is held from Meta Class since Guzdial side Class variables are not analogous to Class methods so then would they reside in the instances of the class?
Robert Schierholz
You're right that the class variables don't reside in the instantiations of the MetaClass. The main reason for this is the inaccessibility of the variables, since the instantiation of the class is not an instantiation of the subclass of the class MetaClass, which contains the class variables for the class.

I'm taking a guess that the class variables are held in some sort of dictionary that all the instantiations of MetaClass have access to. This is quite a quandary, since what I just described is a class variable of the class MetaClass. A sort of chicken and the egg problem.
Christopher Henke
By "instance of MetaClass", are we talking about an actual instance of MetaClass or an instance of one of it's subclasses. I was under the impression that MetaClass was an abstract class and so could never be instantiated.
Shetu Shah



Link to this Page