Thursday, March 24, 2011

What is the difference between Type and Class?

What makes a type different from class and vice versa?

(In the general language-agnostic sense)

From stackoverflow
  • Type is conceptually a superset of class. In the broader sense, a class is one form of type.

    Closely related to classes are interfaces, which can bee seen as a very special kind of class - a purely abstract one. These too are types.

    So "type" encompasses classes, interfaces and in most languages primitives too. Also platforms like the dot-net CLR have structure types too.

    yesraaj : they are different in java
    aku : rajKumar, your question is quite ambiguous. do you as about "type" as a feature of some language, or as a general concept?
    yesraaj : as a general concept
    jalf : Not all user-defined types are classes though, at least not in all languages.
    aku : jalf, agree it is a wrong characteristics. interface is user-defined too, and there can be no user-defined types. Class is a specialized type serving special needs (creating instances of objects)
    Software Monkey : An interface is simply a special type, a purely abstract class - it's still a type (in the greater sense).
    aku : Software Monkey, interface is not a purely abstract class - it is a special concept. "user-defined" is not a defining property of classes
    Software Monkey : It's a *specialized* kind of class, but it is still a kind of class. (Java even compiles an interface to a class object with the same internal structure because it's a difference in conceptual program design, not in actuality).
    aku : Software Monkey, it is hard to argue but I wouldn't treat interface as a subset of classes. IMO defining property of class is ability to create objects, interfaces can not be used for it. Both of them can define contracts but interface can't define behavior.
    Software Monkey : @Aku: Fair enough; but you must concur that they both define types.
    aku : @Software Monkey, yes interfaces and classes are types and can be used to define new types. Only reason I commented on you post is that I don't agree that "user-defined" can be used as a defining characteristic.
    Software Monkey : @Aku: And I edited my answer to clarify that. I never intended to say being user-defined is a defining characteristic - you inferred that.
    aku : @Software Monkey it is my mistake then. Talking about low-level conceptions is never easy :)
    Software Monkey : @Aku: But very helpful I find - one's thinking is, challenged, clarified, and often altered.
  • Type contains description of the data (i.e. properties, operations, etc),

    Class is a specific type - it is a template to create instances of objects.

    Strictly speaking class is a special concept, it can be seen as a package containing subset of metadata describing some aspects of an object.

    For example in C# you can find interfaces and classes. Both of them are types, but interface can only define some contract and can not be instantiated unlike classes.

    Simply speaking class is a specialized type used to encapsulate properties and behavior of an object.

    Wikipedia can give you a more complete answer:

  • I always think of a 'type' as an umbrella term for 'classes' and 'primitives'.

    int foo; // Type is int, class is nonexistent.

    MyClass foo; // Type is MyClass, class is MyClass

    aku : nice and concise explanation :)
    dalle : Well, in .NET it should be the same, even primitives are classes (or more exactly structs).
    Robert Gould : @dalle: agreed, there is no inherent difference between type and class. Eddie's example is very C++/Java dependent. It's not at all THE definition.
    Eddie Parker : I imagine it will be hard to get "THE" definition of a 'type' versus a class. So many languages have their own typing system. One definition I heard for .NET was that a 'type' includes both ref and value types, whereas a class is only used to describe ref types.
    Svish : Isn't int just a short-hand for System.Int32 kind of? In other words: int foo; // Type is int, class is System.Int32 ?
  • To illustrate it the fastest way:

    A Struct is a Type, but a Struct is not a Class.

    As you can see, a Type is an "abstract" term for not only definitions of classes, but also structs and primitive data types like float, int, bool.

    aku : forget CLR it is language-agnostic question :)
    icelava : Ok. Reference removed.
    aku : I would remove references to heap too, it has nothing to do with explanation of type vs class
    icelava : Let me try a different approach to the explanation.
  • Type is the umbrella term for all the available object templates or concepts. A class is one such object template. So is the structure type, the Integer type, the Interface type etc. These are all types

    If you want, you can look at it this way: A type is the parent concept. All the other concepts: Class, Interface, Structure, Integer etc inherit from this concept.i.e They are types

  • I think of a type as being the set of things you can do with a particular value. For instance, if you have an integer value, you can add it to other integers (or perform other arithmetic operations), or pass it to functions which accept an integer argument. If you have an object value, you can call methods on it that are defined by its class.

    Because a class defines what you can do with objects of that class, a class defines a type. A class is more than that though, since it also provides a description of how the methods are implemented (something not implied by the type) and how the fields of the object are laid out.

    Note also that an object value can only have one class, but it may have multiple types, since every superclass provides a subset of the functionality available in the object's class.

    So although objects and types are closely related, they are really not the same thing.

  • Type generally refers to the classification of primitive values - integers, strings, arrays, booleans, null, etc. Usually, you can't create any new types.

    Class refers to the named set of properties and methods which an object is associated with when it is created. You can usually define as many new classes as you want, although some languages you have to create a new object and then attach methods to it.

    This definition is mostly true, but some languages have attempted to combine types and classes in various ways, with various beneficial results.

  • To add another example of distinction: in C++ you have pointer and reference types which can refer to classes, but are not classes in and of themselves.

    Bar b; // b is of type "class Bar"
    Bar *b2 = &b; // b2 is of type "pointer to Class Bar"
    Bar &b3 = b; // b3 is of type "reference to Class Bar"
    Bar *b4[7]; // b4 is of type "7-element array of pointers to Class Bar"
    Bar ***b5; //b5 is of type "pointer to a pointer to a pointer to Class Bar"
    

    Note that only one class is involved, but a near infinite number of types can be used. In some languages, function are considered "first-class-objects" in which case, the type of a function is a class. In others, the type of a function is merely a pointer. Classes generally have the concepts of being able to hold data, as well as operations on that data.

  • Interesting question. I think aku's answer is spot on. Take the java ArrayList class as an example

    public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable
    

    An instance of the ArrayList class is said to be of type of every superclass it extends and every interface it implements. Therefore, an instance of the ArrayList class has a type ArrayList, RandomAccess, Cloneable, and so forth. In other words, values (or instances) belong to one or more types, classes define what these types are.

  • Types and classes are related but not identical. My take is that classes are used for implementation inheritance, whereas types are used for runtime substitution.

    Here is a link explaining the substitution principle and why subclasses and subtypes are not always the same thing (in Java for example). The wikipedia page on covariance and contravariance has more information on this distinction.

  • The following answer is from Gof book (Design Patterns )

    An objects's class defines how the object is implemented .The class defines object's internal state and the implementation of its operations.

    In contrast, an objects's type only refers to its interface -the set of requests to which it can respond.

    An object can have many type, and object of different classes can have the same type.

    //example in c++
    template<typename T> 
    const T & max(T const & a,T const &b)
    {
    return a>b?a:b;  //> operator of the type is used for comparision
    }
    

    max function requires a type with operation > with its own type as one of it interface any class that satisfies the above requirement can be used to generate specific max function for that class.

0 comments:

Post a Comment