CSC/ECE 517 Fall 2007/wiki2 6 mxz

From Expertiza_Wiki
Jump to navigation Jump to search

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.


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


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:

public interface Colorable { void setColor(byte r, byte g, byte b); } class Point { int x, y; } class ColoredPoint extends Point implements Colorable { byte r, g, b; public void setColor(byte rv, byte gv, byte bv) { r = rv; g = gv; b = bv; } } class Test { public static void main(String[] args) { Point p = new Point(); ColoredPoint cp = new ColoredPoint(); p = cp; Colorable c = cp; } }

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.

Every array also has a class; the method getClass, when invoked for an array object, will return a class object (of class Class) that represents the class of the array. The classes for arrays have strange names that are not valid identifiers; for example, the class for an array of int components has the name "[I" and so the value of the expression:

[[new int[10].getClass().getName()]]

is the string "[I"; see the specification of Class.getName for details

Type and Class in Ruby