View this PageEdit this Page (locked)Attachments 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 - Ejike Onyekwuluje

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.What's a class? What's an instance?
A class exists when information is shared by grouping together similar objects that represent the same kind of entity. This way of grouping similar objects not only save memory, but it also provides a way of re-using code.

In OO programming system, every object belongs to one class. The object is called an instance of that class that it belongs to. Also every object contains a reference to the class of which it is an instance.

Therefore, the class of an object acts like a blueprint to determine the number of internal variables an instance will have, and holds a list of methods, which corresponds to the messages to which all instances of the class, will respond.

Objects obtain their behavior from classes; otherwise, each object will carry with it a copy of all the messages to which it understands, and the corresponding methods to those messages - may create efficiency problems. A class therefore determines the behavior of an object.

2. What's the difference between a class and instance method?
Regardless of whether the instances of a class exist or not, class methods are understood by the class objects only. It does not directly affect any instance of an object. Instance methods, on the other hand, are understood by instances only: they only alter the behavior of an actual instance of an object.

3.What's the difference between a class and instance variable?
Class variables are shared by all instances of a single class. If a class has a particular class variable, all instances of that class objects will share the same variable field.
Instance variables, on the other hand, are only available to the specific object in which they are contained. They are simply names for pointers to other objects and always do not exist unless an instance is created.

4.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.


Links to this Page