tags:

views:

63

answers:

3

Hi all, i know this question has been already asked, but i didnt get it quite right, i would like to know, which is the base one, class or the type. I have few questions, please clear those for me,

  1. Is type the base of a programing data type?
  2. type is hard coded into the language itself. Class is something we can define ourselves?
  3. What is untyped languages, please give some examples
  4. type is not something that fall in to the oop concepts, I mean it is not restricted to oop world

Please clear this for me, thanks.

+1  A: 

It may well depend on the language. I treat types and classes as the same thing in OO, only making a distinction between class (the definition of a family of objects) and instance (or object), specific concrete occurrences of a class.

I come originally from a C world where there was no real difference between language-defined types like int and types that you made yourself with typedef or struct.

Likewise, in C++, there's little difference (probably none) between std::string and any class you put together yourself, other than the fact that std::string will almost certainly be bug-free by now. The same isn't always necessary in our own code :-)

I've heard people suggest that types are classes without methods but I don't believe that distinction (again because of my C/C++ background).

There is a fundamental difference in some languages between integral (in the sense of integrated rather than integer) types and class types. Classes can be extended but int and float (examples for C++) cannot.

paxdiablo
Also thanks for the helpful answer.
srisar
+1  A: 

I didn't work with many languages. Maybe, my questions are correct in terms of : Java, C#, Objective-C

1/ I think type is actually data type in some way people talk about it.

2/ No. Both type and class we can define it. An object of Class A has type A. For example if we define String s = "123"; then s has a type String, belong to class String. But the vice versa is not correct.

For example:

class B {}
class A extends B {}
B b = new A();

then you can say b has type B and belong to both class A and B. But b doesn't have type A.

3/ untyped language is a language that allows you to change the type of the variable, like in javascript.

var s = "123";  // type string
s = 123; // then type integer

4/ I don't know much but I think it is not restricted to oop. It can be procedural programming as well

vodkhang
thanks for the great reply
srisar
So, if you think that answers are helpful. Just vote it up
vodkhang
+1  A: 

In OOP languages, a class specifies the definition of an object. In many cases, that object can serve as a type for things like parameter matching in a function.

So, for an example, when you define a function, you specify the type of data that should be passed to the function and the type of data that is returned:

int AddOne(int value) { return value+1; } uses int types for the return value and the parameter being passed in.

In languages that have both, the concepts of type and class/object can almost become interchangeable. However, there are many languages that do not have both. For instance, I believe that standard C has no support for custom-defined objects, but it certainly does still have types. On the otherhand, both PHP and Javascript are examples of languages where type is very loosely defined (basically, types are either single item, collection/array/object, or undefined [js only]), but they have full support for classes/objects.

Another key difference: you can have methods and custom-functions associated with a class/object, but not with a standard data-type.


Hopefully that clarified some. To answer your specific questions:

  1. In some ways, type could be considered a base concept of programming, yes.
  2. Yes, with the exception that classes can be treated as types in functions, as in the example above.
  3. An untyped language is one that lets you use any type of variable interchangeably. Meaning that you can handle a string with the same code that handles an int, for instance. In practice most 'untyped' languages actually implement a concept called duck-typing, so named because they say that 'if it acts like a duck, it should be treated like a duck' and attempt to use any variable as the type that makes sense for the code encountered. Again, php and javascript are two languages which do this.
  4. Very true, type is applicable outside of the OOP world.
JGB146
Thanks for the helpful answer
srisar