What is the difference between Type and Class?
Solution 1
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.
Solution 2
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
Solution 3
Inspired by Wikipedia...
In type theory terms;
A type is an abstract interface.
Types generally represent nouns, such as a person, place or thing, or something nominalized,-
A class represents an implementation of the type.
It is a concrete data structure and collection of subroutinesDifferent concrete classes can produce objects of the same abstract type (depending on type system).
*For example, one might implement the type
Stack
with two classes:SmallStack
(fast for small stacks, but scales poorly) andScalableStack
(scales well but high overhead for small stacks).*Similarly, a given class may have several different constructors.
![]()
The banana example.
A
Banana
type would represent the properties and functionality of bananas in general.The
ABCBanana
andXYZBanana
classes would represent ways of producing bananas.
(Different banana suppliers in real life, or different data structures and functions to represent and draw bananas in a video game).The
ABCBanana
class could then produce particular bananas which are instances of theABCBanana
class, they would be objects of type Banana.
It is not rare the programmer provide a single and only implementation for a type. In this case the class name is often identical with the type name. But there is still a type (which could be extracted in an interface if required), and an implementation (which would implement the separate interface) which builds instances (objects) of the class.
Solution 4
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
Solution 5
Taken from the GoF citation from below:
An objects's class defines how the object is implemented .The class defines the 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.
I want to provide an example using Java:
public interface IType {
}
public class A implements IType {
public A{};
}
public class B implements IType {
public B{};
}
Both classes A
and B
implement the interface and thus are of the type IType
. Additionally in Java, both classes produce their own type (respectively to their class name). Thus the class A
is of type A
and IType
and the class B
is of type B
and IType
satisfying:
An object can have many types, and objects of different classes can have the same type.
The difference between subtypes and subclass probably helps to understand that issue as well:
https://www.cs.princeton.edu/courses/archive/fall98/cs441/mainus/node12.html
Related videos on Youtube
Comments
-
yesraaj almost 2 years
What makes a type different from class and vice versa?
(In the general language-agnostic sense)
-
Brent81 almost 12 yearsEffective C++, Item 19: Treat class design as type design.
-
-
aku over 15 yearsrajKumar, your question is quite ambiguous. do you as about "type" as a feature of some language, or as a general concept?
-
josesuero over 15 yearsNot all user-defined types are classes though, at least not in all languages.
-
aku over 15 yearsjalf, 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)
-
Lawrence Dol over 15 yearsAn interface is simply a special type, a purely abstract class - it's still a type (in the greater sense).
-
aku over 15 yearsSoftware Monkey, interface is not a purely abstract class - it is a special concept. "user-defined" is not a defining property of classes
-
Lawrence Dol over 15 yearsIt'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 over 15 yearsSoftware 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.
-
Lawrence Dol over 15 years@Aku: Fair enough; but you must concur that they both define types.
-
aku over 15 years@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.
-
Lawrence Dol over 15 years@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 over 15 years@Software Monkey it is my mistake then. Talking about low-level conceptions is never easy :)
-
Lawrence Dol over 15 years@Aku: But very helpful I find - one's thinking is, challenged, clarified, and often altered.
-
dalle over 15 yearsWell, in .NET it should be the same, even primitives are classes (or more exactly structs).
-
Robert Gould over 15 years@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.
-
StefanTflch over 15 yearsI 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.
-
Lemon about 15 yearsIsn't int just a short-hand for System.Int32 kind of? In other words: int foo; // Type is int, class is System.Int32 ?
-
jk_ almost 13 yearsWhat if there are no primitives, e.g. in Smalltalk. What's difference between class and type then?
-
James Iry almost 12 yearsIn even the type impoverished language C you can create new types, but it has nothing like what people normally think of as classes except in so much as structs, records, and classes all sort-of resemble each other.
-
supercat over 11 yearsIt would be fine to mention the .net CLR as an example of a framework in which there exist types that are not classes (Java could be cited as another, though .net has more kinds of types). An extra little wrinkle in .net, though, is that
Type
(capitalized as shown) is the short name of a system class (System.Type
) which is used to hold descriptions of types. -
Nick over 5 yearsWelcome to SO. This answer is very similar to at least one other and SO users prefer more technical language than 'thingy'!