-4

a class in java (or in other oops languages) represent a data structure. every data structure has a type. so why one use CLASS keyword ? why can't TYPE keyword ?

i.e

we define new data structure in java like below -

public class CustomDataType { 
    //bla bla
}

why cant it be defined as below -

public type CustomDataType {
//bla bla
}

Please explain.

Atul
  • 521
  • 1
  • 7
  • 20
  • Because `type` is not a Java keyword. – Stephen C Nov 13 '17 at 09:12
  • Quoting @mins from https://stackoverflow.com/questions/468145/what-is-the-difference-between-type-and-class 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 –  Nov 13 '17 at 09:12
  • And ... guess what ... there are lots of other OO languages where `type` is not a keyword either. C++, python, ruby, javascript, Smalltalk (though that only has 6 keywords!) ... – Stephen C Nov 13 '17 at 09:20
  • And ... type is too broad a term because "type" covers primitive data types, which in Java are NOT object / OO types. – Stephen C Nov 13 '17 at 09:22

1 Answers1

1

First answer: It's just a name. When you learn a (natural or programming) language, you have to accept the vocabulary that's in use.

Second answer: In my opinion, choosing the word "class" over "type" was a good decision. Pre-OOP languages like PASCAL or C already had "types", meaning static, open data structures. The OOP idea is that the data structure used to implement the type is just an implementation detail, but the operations you can do with the objects are the important aspect. And inheritance was rarely found / used in pre-OOP programming, but is at the very core of OOP.

So choosing a new term "class" made people aware that there were some really new concepts in the new languages.

And still today there are developers that didn't get it and use classes just like structures from the old days...

Ralf Kleberhoff
  • 6,990
  • 1
  • 13
  • 7