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

Sp2001 Midterm Review: O-O Systems

Questions on O-O Systems:

Class - A representation of an object and its properties. An object is somethig that either exists physically or exists only as a model for other objects. i.e a Blueprint for an object

Instance - A prototype of a class. It contains all the properties of its class and knows how to perform actions that are defined in its class.

Difference between a class and an instance method...
A class method affects the behavior of the class directly. It can modify class behavior or define class behavior.
Instance methods can only perform actions within the bounds of the class. It can influence only its instance of the class and not all classes of that type, i.e, In sketchpad we can design a house(a class). We can make many houses with the same design (instances). If we change the house by adding a chimney to the master house(class method) all instances of the house will get chimneys. However if one particular house is painted yellow(instance method) all houses will not be painted yellow.

Class variable and instance variable.
A class variable is similar to a static variable in JAVA. It can be referenced directly. eg. If number of houses is a c;ass variable of a house we can make as many houses(instances) as we want. Each class will therefore know how many of its kind exists. We can access the number of houses from any class and it should be the same.
However instance variables cannot be referenced unless an instance of a class is created. Also the instance variable will pertain for its instance only. eg. Color of house can be an instance variable. So each house(instance) will have a different value. Also to have a house color we need to have a house.
Bharath Hemachandran

Instance definition, class method definition, and class variable definition are wrong. Mark Guzdial

Another Attempt:

Class - A definition/blueprint of how to create a certain object.

Instance - An object created from a class definition.

Difference Between class and instance methods - A class method is only accessable when the object is created... in other words its the "constructor method" for that class. When you create a new class you can call class methods to specify different options in how the object will be created. Instance methods on the other hand are methods that are handled by all objects of that particular class. Also, once an object is created from a class you can no longer call class methods for that object.

Difference between class variables and instance variables - I don't really understand why Bharath's definition of a class variable is wrong. I guess class variables are just global variables available to all instances of that class, where as instance variables are only available to those instances of the object...

Please verify the correctness/incorrectness of this answer. Thanks.
Arkady Shraybman

Class methods - You can call them any time (both before and after object creation)
Instance methods - can call only after object's creation

Class variables - each instance of the object has access to the same copy of the variable (same memory location)
Instance variable - each instance has their own variable that has separate memory allocate for it

Dmitri

Dmitri,

After you create an object from a class(which is also an object in squeak but we wont worry about that for now) you can no longer call class methods for that object. Example:
a := Pen new. a example.
This code doesnt work because a which is an instance of the object pen does not understand that method call.


You can't call it using the instance because it doesn't have that method, but you can certainly call the class' method any number of times you want to. For instance, a class method does not necessarily have to serve as a constructor (allthough, that's what most of them do). For instance, using P2 as an example - Jukebox has a start method. It does not have to create a new instance of JukeBox. What it needs to do is create a player, playlist, and so on. It never has to call "super new" to create a new JukeBox if it doesn't need to do that. After you call start once, you can certainly do it again any number of times.
Essentially, a class method is a static method (using C++/Java terminology) that is available to everyone, not just instances of that class.

Dmitri



This discussion is getting kind of long. Allow me to add my 2 cents' worth here.

(a) What's a class? What's an instance?
class - blueprint with properties such as attributes and methods, instance - a copy based on the blueprint

(b) What's the difference between a class and instance method?
class mtd - static, instance mtd - can only be called after an instance is created, generally, an instance can call class mtds, but class cannot call instance mtds (becos the class doesn't know whether the instance is created or not and where it is created).

The main differentiating factor is that class mtds are static while instance mtds are not. Other subtle differences, such as whether instance mtds can change class variables or not, are pretty much subjective. (according to Mark, class vars & mtds are only visible to class things, instance vars & mtds are only visible to instance things, not visible vice-versa, but tat's not true for C++ and Java!)

(c) What's the difference between a class and instance variable?
class var - already there (static), no need to declare
instance var - created together with instance

(d) What are the strengths and weaknesses of a prototype vs. class-based object system?

strength of proto - usually one or two levels of abstraction (is tat a strength?), so code runs faster with less overhead on function calls (however insignificant the overhead might be)
weakness of proto - code is reusable only to a certain extent

OO strength - highly reusable code, so able to create more complicated applications more quickly
OO weakness - may be slower

chok





Link to this Page