4

How does an object oriented programmer understand the word "type"? I'm not aware of there being a type theory for object oriented programming because whenever someone mentions type theory it is always in the context of some kind of functional programming language so what does "type" mean in the object oriented context?

1
  • type is what you do when you want to write a class ;-) Jan 6, 2011 at 18:58

4 Answers 4

4

Type is basically the same as class - it defines characteristics and behavior of a family of objects. You instantiate types to create objects of those types.

In practice, types usually refer to a set of predefined classes supplied with the class library of a programming environment - basic primitive data types and structures. Still, most of the time you can use the terms type and class interchangeably.

4
  • So you're saying 'type' = 'class'.
    – user7146
    Jan 6, 2011 at 17:50
  • 4
    Of course, in Java (and C#?) type can refer to an interface. So it is a little more broad than your answer indicates. Jan 6, 2011 at 18:03
  • 1
    A type is not at all the same thing as a class. A class is a type, but a type is not a class. A type can refer to any data object include primitives such int
    – Pemdas
    Jan 6, 2011 at 18:39
  • 1
    In most languages the relation is type \superset class, not type = class. For example in C++ enums, typedefs, primitive types and classes are all types, while only classes are classes.
    – sepp2k
    Jan 6, 2011 at 18:40
2

A type refers to the identity of a variable. Some variables are integers, some are strings, some are arrays, some are objects, etc. When you have objects, an object's "type" is basically the same concept as its "class," and some type theory starts to be introduced there because you have to deal with inheritance and type substitution: if a variable class descends from another class, and a function expects an instance of the base class as a parameter, you need to be able to pass an instance of a descendant class to the function and have the compiler be able to treat it as if it were the base class that it descends from.

1

In object-orientation, types are the exact same thing as everywhere else. Todays mainstream type-systems are just extensions of what we used to have in C/C++.

The reason you hear a lot about types and functional programming is that these people (particularly the haskell-community) are trying to do types well.

Even mainstream languages have reasonably advanced type systems, just look at generics. Generics are all about types. Casts are about types.

A few examples of types in Java:

  • Int i is of type Int
  • A Cat is a subclass and therefor subtype of Animal (this assumption is broken if you violate the Liskov substitution principle, but that's a matter for another rant)
  • A cat of class Cat is of type Cat and of type Animal
  • An array of cats is also an array of animals, but that only works as long as you only take animals from the array, putting a dog into this array will not work, although dogs are also animals. Java's assumption that arrays are covariant holds only for reads, writes are contravariant.
  • A flyingCat that implements the interface Flying is of type Flying
0

A class is a advanced date type. Typically, you will see references to primitive data type, which include data types such as int, float, char...ect. Then there are more advanced data types such enums structures and unions. Historically, data types could only represent data. The concept of the class expended this to allow a data type that can include function definitions as well access specifiers, which placed restrictions or access rights to individual "members" of the class. A class is one of the most advanced data types available. The main difference is that object oriented programing language allow for polymorphism, which allows values of different data types to be handle by the same interface. You can fake a form of polymorphism in structured languages such as C, but it is not formally supported by the language. An object is an instance of class. An object is not a type.

In short, it means exactly the same thing as it alway has.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.