Hotspots: Admin Pages | Turn-in Site |
Current Links: Cases Final Project Summer 2007
Discussion 4 - Samuel Young
Spring 2004 Midterm #1
A class is a set of instructions that define an object. It is like a blueprint for the object. An instance is an actual, existing object. It is an entity in and of itself and follows the blueprint provided by its class.
- What's a class? What's an instance?
An class method is associated with the class and does not directly affect any instance of an object. It may be called by asserting only the class name. On the other hand, an instance method alters the behavior of an actual instance of an object. It may only be called through an instance of an object.
- What's the difference between a class and instance method?
A class variable exists across all instances of a given class. If class A has a class variable Var, all instances of A objects share the same Var field. On the other hand, instance variables are specific to each instance of an object and do not exist unless an instance is created. If class A defines an instance variable X, then an instance of A named One and and instance of A named Two have distinctly different variables X and X respective values are not related.
- What's the difference between a class and instance variable?
- In the following Squeak code what type of method (class or instance) is "new" and what type is "initialize"?
joe := Box new initialize
'new' is a class method. The class Box is the only assertion required to send the message 'new'. 'new' is sent to class Box, and class Box creates and returns an instance of itself.
'initialize' is an instance method. Since 'new' returns an instance of class Box, it is this instance (not the class) that is sent the 'initialize' message.
Link to this Page