CSC/ECE 517 Fall 2007/wiki2 6 ap
Types vs Classes
In object oriented languages, type defines the common features of a set of similar objects. A 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.
The implementation comprises 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.
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.
Types vs Classes in Python
Python 2.2 unifies types and classes by removing most of the differences between the in-built types and user-defined classes. This is done by not allowing the use of built-in types as base classes in class declaration statements.
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/