What makes a type different from class and vice versa?
(In the general language-agnostic sense)
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.
Different classes may describe the same type.
Type consists of these parts:
Class consists of these parts:
Some notes:
Interface (as in Java) is not type, because it does not describe semantics (describes only syntax)
Subclass is not subtype, because subclass may change semantics defined in superclass, subtype cannot change supertype semantics (see Liskov Substitution Principle, e.g. this LSP example).
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 subroutines
Different 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) and ScalableStack
(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.
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.
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
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