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

From Expertiza_Wiki
Jump to navigation Jump to search
Line 96: Line 96:


== Phyton ==
== Phyton ==
Phyton is an important example. In earlier versions of Phyton derived types were not created from built in types but just from user defined classes. After Phyton 2.2 this has changed and "type/class unification" is accomplished. That is like other O-O languages every type started mapping on a single class. Python intended to remove most of the differences between built-in types and user-defined classes. Perhaps the most obvious one is the restriction against using built-in types (such as the type of lists and dictionaries) as a base class in a class statement.
Even though Phyton has made changes there are still differences with Ruby which is also dynamically typed language. Emulating container types in Phyton is harder because there are different methods you have to use for "Emulating callable Objects", "Emulating numeric types", "Emulating sequences" etc.. In Ruby, you just derive from the respective base classes (or extend them directly). So, Ruby's type system is much simpler and cleaner and allows a smaller and simpler language core.


== Types vs Class in C++ ==
== Types vs Class in C++ ==

Revision as of 17:11, 23 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] A class is the blueprint from which individual objects are created. If we think of instances of classes, that is objects as variables, a class will be the type of the variable.


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.


Classes differ from types in one way that they can have their own class methods and class variables which don't invoke any of its instances. Like we may have a Car Class which creates different kinds of cars. In this class we may have variable counting the number of objects created from that class. But a type does not have such variables holding information of its instances.

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 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. As with several other object-oriented languages the Object class is the root of all types.


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. Class objects in Ruby are not explicitly declared to have a certain 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.


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.

Practical and Syntax Differences

While in Java you have to define the type every time you create a new variable, in Ruby this is not needed as stated above. In Java:

String str = "The Type is defined"

whereas in Ruby we don't need type String be defined:

str = 'No Type is defined'

When you want to change the type of an object in Java you need to type cast it, in Ruby this is not needed.

Phyton

Phyton is an important example. In earlier versions of Phyton derived types were not created from built in types but just from user defined classes. After Phyton 2.2 this has changed and "type/class unification" is accomplished. That is like other O-O languages every type started mapping on a single class. Python intended to remove most of the differences between built-in types and user-defined classes. Perhaps the most obvious one is the restriction against using built-in types (such as the type of lists and dictionaries) as a base class in a class statement.

Even though Phyton has made changes there are still differences with Ruby which is also dynamically typed language. Emulating container types in Phyton is harder because there are different methods you have to use for "Emulating callable Objects", "Emulating numeric types", "Emulating sequences" etc.. In Ruby, you just derive from the respective base classes (or extend them directly). So, Ruby's type system is much simpler and cleaner and allows a smaller and simpler language core.

Types vs Class in C++

C++ like Java has different kinds of types. Types can be built in, user defined or derived from preexisting classes. Built in types are types that are already defined in the language like int or String. User defined types are types created from user written classes. Lastly derived types are types created form extending the other two types of classes. "derived class" in C++ is "subclass" in Java. When a class extends a base class, to derive a new class with all the base's variables and methods, plus some of its own, we call the new class the subclass, and the base class the new class's "superclass".

In C++ like Java every variable has a type at compile time and the variable needs to be type casted if the type of the variable needs to be change.

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

[7] http://mindprod.com/jgloss/derivedclass.html