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

Discussion 4 - G. Stepanov

Midterm #1 Review - Spring 2004

O-O Systems
  1. What's a class? What's an instance?
  2. What's the difference between a class and instance method?
  3. What's the difference between a class and instance variable?
  4. In the following Squeak code what type of method (class or instance) is "new" and what type is "initialize"?
          joe := Box new initialize

1) A class is the template, or blueprint, of an object. It defines what variables and functions the object will have once it is created. An instance is what you get when you actually create an object. It has specific values for the variables and you can use its methods*. For any given class, you can have many different instance (like for any blueprint of a house you can have many different houses actually built).
2) An instance method can only be called on an instance of an object; you need to create an instance before you can use an instance method. Going with the house example, you have to build your house before you can open its door. A class method does not have this requirement: you use the class name to call it. Creation methods are examples of class methods (you can't exactly use an instance method to create an object if you have to have an object created to use an instance method ;)
3) Instance/class variables are similar to instance/class methods. You need to have instances of the class to use instance variables, but not for class variables. In addition, each instance has its own values for the variables and a change to one does not affect any others. There is only one of each class variable per class, though, no matter how many instances you create. A change to the class variable will affect ball/b instances of that object.
4) New is a class method. It is called on the Box class and creates an instances of Box. Initialize is an instance method. It is called on the newly created instance of the Box class and (assuming standard squeak) initializes the values of this new object's variables.

*class methods are an exception to this: they can be used without creating an instance of an object

Links to this Page