Difference Between Class and Type

What is the difference between Type and Class?

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.

Difference between class and type

A class is a type. An interface is a type. A primitive is a type. An array is a type.

Therefore, every type is also either a class (including an enum constant), an interface, a primitive, or an array.

There are two distinct categories of types: primitive types and reference types:

  • A variable of primitive type always holds a primitive value of that same type. Such a value can only be changed by assignment operations on that variable.
  • A variable of reference type always holds the value of a reference to an object. All objects, including arrays, support the methods of class Object. The reference types are class types (including enum types), interface types, and array types.

Every piece of data has a type which defines its structure, namely how much memory it takes up, how it is laid out, and more importantly, how you can interact with it.

Examples of primitive types:

  1. int
  2. float
  3. char
  4. boolean

Examples of class types:

  1. String
  2. Integer
  3. Boolean
  4. ArrayList
  5. StringBuilder

Examples of interface types:

  1. Collection
  2. List
  3. Map
  4. Serializable

Examples of array types:

  1. int[]
  2. String[]
  3. Integer[][][]

Basically, anything that you can refer to as a variable has a type, and classes are a kind of a type.

More info here: http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html

What is the difference between type and class in Typescript?

Typescript has two different universes that come into contact in some points: Value space and Type space. Type space is where types are defined and types get erased completely and don't exist at runtime. Value space contains values and will obviously exist at runtime.

What is a value? Value literals, variables, constants and parameters are obviously values. Functions and class declarations are also values as they do have a runtime object backing them up, namely the function object and the class constructor (also a function). Enums are also values as they are backed up by an object at runtime.

What is a type? Any definition with a type keyword is a type as well as interfaces, class declarations and enums

You will notice I mentioned class declarations in both spaces. Classes exist in both type space, and value space. This is why we can use them both in type annotations (let foo: ClassName) and in expressions (ex new ClassName()).

Enums also span both worlds, they also represent a type we can use in an annotation, but also the runtime object that will hold the enum.

Names in type space and value space don't collide, this is why we can define both a type and a variable with the same name:

type Foo = { type: true }
var Foo = { value : true } // No error, no relation to Foo just have the same name in value space

Class declarations and enums, since they span both spaces will 'use up' the name in both spaces and thus we can't define a variable or a type with the same name as a class declaration or enum (although we can do merging but that is a different concept)

In your specific case, Point is just a type, something we can use in type annotations, not something we can use in expressions that will need to have a runtime presence. In this case the type is useful as it allows the compiler to structurally check that the object literal is assignable to the Point type:

let p: Point = { x: 10, y: 15 }; // OK
let p: Point = { x: 10, y: 15, z: 10 }; // Error

If you want to create a class, you will need to do that with the class keyword, as that will create a runtime value that is not just a type:

class Point{
constructor(public x: number, public y: number){}
}
let p = new Point(10,10)

What is the difference between the class `Class` and the class `Type` in java?

Type is a superinterface of Class. You won't generally need to use it unless you're doing reflection with generic types.

As an example, we can get the type of a field using Field.getType():

Class<?> c =
String.class.getField("CASE_INSENSITIVE_ORDER")
.getType();

The problem is that String.CASE_INSENSITIVE_ORDER is actually a Comparator<String>, and the above code will only get us Comparator.class. This doesn't work for us if we needed to know what the type argument was.

Type and its uses were added in Java 1.5 (along with generics) for this type of situation. We could instead use the method Field.getGenericType():

Type t =
String.class.getField("CASE_INSENSITIVE_ORDER")
.getGenericType();

In this case, it would return an instance of ParameterizedType, with Comparator.class as its raw type and String.class in its type arguments:

ParameterizedType pt = (ParameterizedType) t;
pt.getRawType(); // interface java.util.Comparator
pt.getActualTypeArguments(); // [class java.lang.String]

This part of the API isn't very well developed, but there are some better ones built around it, like Guava TypeToken.

Class vs. Type in Python

Once upon a time, Python had both types and classes. Types were built-in objects defined in C; classes were what you built when using a class statement. The two were named differently because you couldn't mix these; classes could not extend types.

This difference was artificial, a limitation in the language implementation. Starting with Python 2.2, the developers of Python have slowly moved towards unifying the two concepts, with the difference all but gone in Python 3. Built-in types are now also labelled classes, and you can extend them at will.

Your book is trying to explain a difference that isn't present in Python anymore. Even in Python 2 the difference is only there in name, since type(2) shows the word 'type' is still used there:

>>> type(2)
<type 'int'>

but you can subclass int just like any other class.

(Python 2 does still have old-style classes, those that don't inherit from object; these are a remnant of the old system from before the unification.)

What is the difference between a class and a type in Scala (and Java)?

When you say "type" I'm going to assume you mean static type mostly. But I'll talk about dynamic types shortly.

A static type is a property of a portion of a program that can be statically proven (static means "without running it"). In a statically typed language, every expression has a type whether you write it or not. For instance, in the Cish "int x = a * b + c - d", a,b,c,and d have types, a * b has a type, a * b + c has a type and a * b + c -d has a type. But we've only annotated x with a type. In other languages, such as Scala, C#, Haskell, SML, and F#, even that wouldn't be necessary.

Exactly what properties are provable depends on the type checker.

A Scala style class, on the other hand, is just the specification for a set of objects. That specification includes some type information and includes a lot of implementation and representation details such as method bodies and private fields, etc. In Scala a class also specifies some module boundaries.

Many languages have types but don't have classes and many languages have classes but don't have (static) types.

There are several observable differences between types and classes. List[String] is a type but not a class. In Scala List is class but normally not a type (it's actually a higher kinded type). In C# List isn't a type of any sort and in Java it's a "raw type".

Scala offers structural types. {def foo : Bar} means any object that provably has a foo method that returns a Bar, regardless of class. It's a type, but not a class.

Types can be abstracted using type parameters. When you write def foo[T](x : T) = ..., then inside the body of foo T is a type. But T is not a class.

Types can be virtual in Scala (i.e. "abstract type members"), but classes can't be virtual with Scala today (although there's a boilerplate heavy way to encode virtual classes https://wiki.scala-lang.org/display/SIW/VirtualClassesDesign)

Now, dynamic types. Dynamic types are properties of objects that the runtime automatically checks before performing certain operations. In dynamically typed class-based OO languages there's a strong correlation between types and classes. The same thing happens on JVM languages such as Scala and Java which have operations that can only be checked dynamically such as reflection and casting. In those languages, "type erasure" more or less means that the dynamic type of most objects is the same as their class. More or less. That's not true of, e.g., arrays which aren't typically erased so that the runtime can tell the difference between Array[Int] and Array[String]. But remember my broad definition "dynamic types are properties of objects that the runtime automatically checks." When you use reflection it is possible to send any message to any object. If the object supports that message then everything works out. Thus it makes sense to talk of all objects that can quack like a duck as a dynamic type, even though it's not a class. That's the essence of what the Python and Ruby communities call "duck typing." Also, by my broad definition even "zeroness" is a dynamic type in the sense that, in most languages, the runtime automatically checks numbers to make sure you don't divide by zero. There are a very, very few languages that can prove that statically by making zero (or not-zero) a static type.

Finally, as other's have mentioned, there are types like int which don't have a class as an implementation detail, types like Null and Any which are a bit special but COULD have classes and don't, and types like Nothing which doesn't even have any values let alone a class.

What is the difference between a class and a datatype?

C# is a strongly typed language;
therefore every variable and object
must have a declared type.

A data type can be described as being either:

A built-in data type, such as an int
or char, or

A user-defined data type, such as a
class or interface.

Data types can also be defined as
being either:

Value Types (C# Reference), which
store values, or

Reference Types (C# Reference), which
store references to the actual data.

** Class is a user define data type.
**



Related Topics



Leave a reply



Submit