CSC/ECE 517 Fall 2010/ch2 2c ls

From Expertiza_Wiki
Revision as of 04:35, 22 September 2010 by Paullei (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Introduction

In object-oriented programming (oop), inheritance is a way to reuse code by creating collections of attributes and beheaviors called objects, which can be classified as class-based inheritance and prototype-based inheritance. In class-based inheritance, objects are defined by classes which can inherit other classes; and in contrast, in prototype-based inheritance, objects can be defined directly from other objects without the need to define any classes. Particularly, prototype-based inheritance is a code reuse behavior which is performed via a process of cloning existing objects that serve as prototypes.

"Class-based" Vs. "Prototype-Based"

We first compare the whole "class" with "prototype". The idea originally began in Simula, where with a class-based method each class represented a set of objects that share the same state space and the same operations, thereby forming an equivalence class. Later object-oriented languages wanted to be able to use static type checking, so we got the notion of a fixed class set at compile time. In "class-based" inheritance, copying happens at compile time. In prototype-based inheritance, the operations are stored in the prototype data structure, which is copied and modified at run time. Abstractly, though, a class is still the equivalence class of all objects that share the same state space and methods. When you add a method to the prototype, you're effectively making an element of a new equivalence class.

In class-based languages a new instance is constructed through the class's constructor and an optional set of constructor arguments. The resulting instance is modeled on the layout and behavior dictated by the chosen class.

In prototype-based systems there are two methods of constructing new objects, through cloning of an existing object, and through ex nihilo ("from nothing") object creation. While most systems support a variety of cloning, ex nihilo object creation is not as prominent.

Pros for Prototype-base Inheritance

Class-based languages encourage a model of development that focuses first on the taxonomy and relationships between classes. In contrast, prototype-based programming is seen as encouraging the programmer to focus on the behavior of some set of examples and only later worry about classifying these objects into archetypal objects that are later used in a fashion similar to classes. As such, many prototype-based systems encourage the alteration of prototypes during runtime, whereas only very few class-based object-oriented systems (such as the dynamic object-oriented system, Smalltalk, Python, Perl, or Ruby) allow classes to be altered during the execution of a program. Advocates argue the following pros for prototype-based inheritance: 1, Suitable in loosely typed environments, no need to define explicit types. 2, Makes it incredibly easy to implement singleton pattern ( compare javascript and java in this regard.) 3, Provides ways of applying a method of an object in the context of a different object, adding and replacing methods dynamically from an object etc.

Cons for Prototype-base Inheritance

Advocates of class-based inheritance criticize prototype-based inheritance often have concerns that could be seen as similar to those concerns that proponents of static type systems for programming languages have of dynamic type systems (see Datatype). Usually, such concerns involve: correctness, safety, predictability, and efficiency.

References

  • (1) Günther Blaschek, Omega: Statically Typed Prototypes.
  • (2) John C. Mitchell, Concepts in programming languages, Cambridge University Press, 2003, ISBN 0521780985, chapter 10 "Concepts in object-oriented languages"
  • (3) Antero Taivalsaari, Classes vs. Prototypes: Some Philosophical and Historical Observations
  • (4)