CSC/ECE 517 Fall 2007/wiki2 6 mxz: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 74: Line 74:
variable is an object. Also there is no distinction between primitive types and object types. Thats why in Ruby  
variable is an object. Also there is no distinction between primitive types and object types. Thats why in Ruby  
every type maps to a class.
every type maps to a class.


In Ruby an objects type is not determined by which class it belongs to but by what it can do. That is why we can use duck typing Ruby. In Ruby an object is of a certain type if it behaves as that type.
In Ruby an objects type is not determined by which class it belongs to but by what it can do. That is why we can use duck typing Ruby. In Ruby an object is of a certain type if it behaves as that type.


In Ruby the type of a variable depends upon what was last assigned to it. A variable can hold many different types during the lifetime of a script. As with several other object-oriented languages the Object class is the root of all types.
In Ruby the type of a variable depends upon what was last assigned to it. A variable can hold many different types during the lifetime of a script. As with several other object-oriented languages the Object class is the root of all types.


Java uses inheritance as the mechanism for defining the type of an object. An object "is a" X if it implements X. While Ruby also supports inheritance, establishing "is a" relationships is not its main purpose.
Java uses inheritance as the mechanism for defining the type of an object. An object "is a" X if it implements X. While Ruby also supports inheritance, establishing "is a" relationships is not its main purpose.

Revision as of 21:07, 21 October 2007

Assignment 2 - Topic 6

Type v.s. Class

Survey the differences on type vs. class in object-oriented languages. Often, the distinction is that class pertains to an object, whereas type pertains to a variable. Consider the distinction between the two terms in several different programming languages. Cover the differences between type and class, from type-theoretic definitions to practical aspects.


Definitions of Type and Class

Types limit the values that a variable can hold or that an expression can produce, limit the operations supported on those values, and determine the meaning of the operations. Types are corresponding to values through variables, expressions. [3]


A class is exactly the combination of a type, a (possibly trivial) constructor and optionally a subtyping relation. Also, classes are corresponding to objects.[2]


Class pertains to an object, whereas type pertains to a variable. The primary difference is that "type" is generally a compile-time concept. The class of an object, though, exists and is important at runtime, primarily in polymorphism, and in the behavior of casting operations. Of course, in OOP languages, classes are types, while most types are not classes. In pure object languages, by opposition (e.g. Smalltalk, Scala, perhaps Python), every type maps to a class.



Type, Class and Interface in Java

Theoretic Difference

In the Java programming language, every variable and every expression has a type that can be determined at compile time. The type may be a primitive type or a reference type. Reference types include class types and interface types. Reference types are introduced by type declarations, which include class declarations and interface declarations. We often use the term type to refer to either a class or an interface.


Every object belongs to some particular class: the class that was mentioned in the creation expression that produced the object, the class whose Class object was used to invoke a reflective method to produce the object, or the String class for objects implicitly created by the string concatenation operator "+". This class is called the class of the object. An object is said to be an instance of its class and of all superclasses of its class.


Sometimes a variable or expression is said to have a "run-time type". This refers to the class of the object referred to by the value of the variable or expression at run time, assuming that the value is not null.


The compile time type of a variable is always declared, and the compile time type of an expression can be deduced at compile time. The compile time type limits the possible values that the variable can hold or the expression can produce at run time. If a run-time value is a reference that is not null, it refers to an object or array that has a class, and that class will necessarily be compatible with the compile-time type.


Even though a variable or expression may have a compile-time type that is an interface type, there are no instances of interfaces. A variable or expression whose type is an interface type can reference any object whose class implements that interface.


Practical Difference - Example

Here is an example of creating new objects and of the distinction between the type of a variable and the class of an object:



In this example,


The local variable p of the method main of class Test has type Point and is initially assigned
a reference to a new instance of class Point. 
    
The local variable cp similarly has as its type ColoredPoint, and is initially assigned a
reference to a new instance of class ColoredPoint. 

The assignment of the value of cp to the variable p causes p to hold a reference to a
ColoredPoint object. This is permitted because ColoredPoint is a subclass of Point, so the
class ColoredPoint is assignment compatible with the type Point. A ColoredPoint object includes
support for all the methods of a Point. In addition to its particular fields r, g, and b, it
has the fields of class Point, namely x and y. 
    
The local variable c has as its type the interface type Colorable, so it can hold a reference
to any object whose class implements Colorable; specifically, it can hold a reference to a
ColoredPoint. 

Note that an expression such as "new Colorable()" is not valid because it is not possible to
create an instance of an interface, only of a class. [3]

Type and Class in Ruby

Theoretic Differences

We say that objects are instances of classes and variables are instances of types. While this generally holds things are a bit different in Ruby. As Ruby is a purely object oriented programming language every instance or variable is an object. Also there is no distinction between primitive types and object types. Thats why in Ruby every type maps to a class.


In Ruby an objects type is not determined by which class it belongs to but by what it can do. That is why we can use duck typing Ruby. In Ruby an object is of a certain type if it behaves as that type.


In Ruby the type of a variable depends upon what was last assigned to it. A variable can hold many different types during the lifetime of a script. As with several other object-oriented languages the Object class is the root of all types.


Java uses inheritance as the mechanism for defining the type of an object. An object "is a" X if it implements X. While Ruby also supports inheritance, establishing "is a" relationships is not its main purpose.

REFERENCES

[1] http://

[2] http://lambda-the-ultimate.org/node/2079

[3] http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html

[4] http://blog.csdn.net/fat_how/archive/2004/09/09/98768.aspx

[5] http://www.cs.mun.ca/~donald/slug/2003-10-16/

[6] http://alek.xspaces.org/2005/02/27/ruby-type-explosion