CSC/ECE 517 Fall 2007/wiki2 6 ap

From Expertiza_Wiki
Revision as of 21:48, 22 October 2007 by Pnphadni (talk | contribs)
Jump to navigation Jump to search

Types vs Classes

In object oriented programming concepts, the difference between types and classes is not clearly understood. The differences between them are subtle and the two terms can be used interchangeably with respect to object oriented programming concepts. In the article we try to bring out the differences between Types and Classes and provide more insight into them.

Definitions

Definition of a type

A type, in an object-oriented system, summarizes the common features of a set of objects with the same characteristics. It corresponds to the notion of an abstract data type. Type has 2 parts: the interface and the implementation(s). The interface, which is the only part visible to the user, consists of a list of operations and their signatures. The type implementation is of concern only to the type designer.

Definition of a class

A class, in object oriented systems, is a kind of metadata that describes the rules by which objects behave; these objects are referred to as instances of that class. A class specifies the structure of data (a.k.a. properties, fields, attributes, data members) within each instance as well as methods (functions) (a.k.a. behaviors) which manipulate the data of the object and perform tasks.

Types

Types are abstract views that define properties of a set of entities. Object-oriented programming languages must allow to implement these types. Consequently, once a type is implemented we have a particular representation of it available. We will use the seminal concept of the integer data type to explain the concept of type in detail. Programming languages such as C, Java, C++, Pascal and others already offer an implementation for it. Sometimes it is called int or integer. Once you've created a variable of this type you can use its provided operations. For example, you can add two integers:

  int i, j, k;    /* Define three integers */

  i = 1;          /* Assign 1 to integer i */
  j = 2;          /* Assign 2 to integer j */
  k = i + j;      /* Assign the sum of i and j to k */

The first line defines three instances i, j and k of type Integer. Consequently, for each instance the special operation constructor should be called. In our example, this is internally done by the compiler. The compiler reserves memory to hold the value of an integer and binds the corresponding name to it. If you refer to i you actually refer to this memory area which was constructed by the definition of i.

The implementation of types comprises of the data (internal structure of the object’s data) and operation (methods / procedures which implement the interface operations). By making users declare the types of variables at compile time, the system simplifies the procedure of checking the program correctness at compile time, thereby increasing programmer productivity. The types cannot be modified at run-time.

Properties of a Type

  • It exports a set of operations. This set is called interface.
  • Operations of the interface are the one and only access mechanism to the type's data structure.
  • Axioms and preconditions define the application domain of the type.


Classes

A class is an actual representation of a type. It therefore provides implementation details for the data structure used and operations specified in the type. Now we will define a class that implements the Integer data type:

  
class Integer {
  attributes: || implementation : 
    int i /* Integer data type */

  methods: || operation :
    setValue(int n) /* To assign value to i */
    Integer addValue(Integer j) /* To add integer j with i */
  }

In this notation class {...} denotes the definition of a class. Enclosed in the curly brackets are two sections attributes: and methods: which define the implementation of the data structure and operations of the corresponding class. Again we distinguish the two levels with different terms: At the implementation level we speak of ``attributes which are elements of the data structure at the class level. The same applies to ``methods which are the implementation of the class operations. In our example, the attribute is an ordinary integer of a programming language . We only define two methods setValue() and addValue() representing the two of the many operations that can be performed on an integer data type set and add, to make the comparison succinct and concise.

Although the specification of a class is pretty similar to that of a type, it is more of a runtime notion. There are two major aspects of a class – an object factory and an object warehouse. The object factory is the means by which objects of a class are created, using the new operation on the class. Object warehouse stores the set of objects that are instances of the class. Users can work with all these objects by performing all the allowed operations on them and thereby change the contents of the object warehouse. Thus, classes by no means determine the correctness of a program, rather they are used to create and manipulate objects, thereby making the system more flexible.


Types vs Classes in Java


Java is a strongly typed language i.e. the type of every variable and expression used in a program must be known at compile time. The type of a variable / expression defines the range of values that it can hold. The type of a variable also determines the operations that can be performed on that variable. The strong typing nature of Java enables it to determine errors at compile time. The 2 types in Java are – primitive types and reference types. The primitive types are boolean and numeric. The reference types are class types, interface types and array types.

Whenever a variable is declared in Java, we need to mention its type, which may be a class, interface or a primitive. When an object of a subclass of some other class is instantiated, we can say that the object is of type subclass and type superclass and it can perform all operations of both the subclass and the superclass.


Types vs Classes in Ruby


In Ruby, all classes are instances of the class Class. Whenever a new class is defined, an object of type Class is created and assigned to the ‘Name’ of the class. ‘Name.new’ is used to create a new object of the class. This invokes the ‘new’ instance method in Class, which in turn calls the ‘allocate’ method to allocate memory for the object, before finally calling the new object's initialize method. Thus, the construction of an object is separate from its initialization, and both can be over-ridden. The initialization is done via the ‘initialize’ instance method while the construction is done via the ‘new’ class method.

A class is a combination of state (data) and behavior (methods) that manipulate the state. A class is a blueprint for an object.


References:


http://www.cs.cmu.edu/People/clamen/OODBMS/Manifesto/htManifesto/node6.html
http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html
http://rubylearning.com/satishtalim/writing_our_own_class_in_ruby.html
http://www.python.org/download/releases/2.2/descrintro/