Search code examples
language-agnosticclassprogramming-languagestypesglossary

What is the difference between Type and Class?


What makes a type different from class and vice versa?

(In the general language-agnostic sense)


Solution

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

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

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

    An object can have many types, and objects 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 comparison
    }
    

    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<particular class/primitive type> function for that class.