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

CoWebAssignment2 gtg994h

Prototype vs. Class Based Inheritance

In class based inheritance an object is defined based on a predefined type. The type can have sub and super classes. Depending on its super classes it inherits variables and methods. Some of these can be overriden.

With prototype based inheritance, an object or instance is cloned to create new objects. So yes it inherits from the original, but it is exactly the same as the original(initially). A new instance of a prototype is able to change everything, add new things, and remove anything (methods, variables, etc.). Essentially we are copying an instance and then modyifing it to fit our needs.

Prototype inheritance is useful in areas such as gui's where various parts of the gui work roughly the same but just need slight changes. You have created a button and just need to create another button that has different text on it. Rather than using Class inheritance and having to re-enter all the data in the constuctor, just create a new instance of the prototype and modify as needed.

Class based inheritance is more important when you are trying to preserve the underlying relationships and create a more realistic model. It allows you to enforce what can and cannot be changed (as opposed to prototype where you can change everything) so that the user must abide by certain class relationships.

Tracing Code:

1 + 2 3 - 4 factorial
yields: -15

a := #(1 2 3 4 5)
yields: #(1 2 3 4 5)

a select: [:i | i odd]
yields: #(1 3 5)

yields: #(1 2 3 4 5)

a := a reversed
yields #(5 4 3 2 1)

b collect: [:i | i i]
yields: #(1 4 9 16 25)

a perform: #at: withArguments: #(4)
yields: 2

Link to this Page