Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Thanks type and gentle class

Type and class are not interchangeable terms, but two carefully distinguishable object-oriented concepts

  • Print
  • Feedback
In Shakespeare's Hamlet, the king and queen of Denmark address two of Hamlet's buddies with "Thanks Rosencratz and gentle Guildenstern" and "Thanks Guildenstern and gentle Rosencratz." Critics reference these lines to declare the two fellows as indistinguishable, perhaps with an eye toward their function in the play.

And so it is with type and class. In the seminal work Object Oriented Analysis and Design with Applications, (Addison Wesley, 1994), Grady Booch declares, "For our purposes, we will use the terms type and class interchangeably." A footnote goes on to explain that, "A type and a class are not exactly the same ... For most mortals, however, separating the concepts of type and class is utterly confusing."

Certainly Booch deserves the eminent status he has attained in the object-oriented community. Nonetheless, I disagree with his assessment, though in fairness I should acknowledge that he speaks from a language-independent view. Fortunately for Java developers, the Java language has taken a positive step in facilitating a clearer distinction between type and class. To explore that distinction, we will examine type and class from a Java perspective.

The advantage of type

Programming language types characterize the values used in the course of program execution. Though limited compared with modern languages, FORTRAN first introduced types in 1954. The language syntax allowed programmers to distinguish between numeric types for integer and floating-point arithmetic. Variables beginning with those letters between i and n were implicitly typed as integers. Relics of that convention survive today, as many programmers still use the letters i and j for array subscripting.

The FORTRAN typing scheme's primary benefit was code optimization for the underlying hardware system. Computer language evolution quickly raised type from its restricted realm of urging faster code production from compilers to allowing users to define their own data types. User-defined types are the pragmatic extension of abstract data types from type theory. An abstract data type is an ordinary type along with a set of operations. Abstract data types effectively shield module internal mechanics by permitting interaction only through published type operations. You will recognize the class concept as the manifestation of abstract data types in object-oriented languages. An object-oriented class is an abstract data type with full or partial implementation of each declared type operation.

User-defined data types realize significant benefit in extending a programming language's primitive type system. Expressive combinations of primitive and user-defined types create new, more complex types. Importantly, these user-defined types are first-class citizens, meaning that objects characterized by these types enjoy privileges similar to those of primitive types, thereby facilitating efficient data structure management.

The introduction of programming language types also paved the way for program text type-checking. Type operations restrict the permissible interaction with a user-defined type, thereby declaring the explicit boundaries of a system module. A type-checker, based on well-defined typing rules, enforces the proper use of program types by ensuring the integrity of these boundaries. Type-checking's primary purpose is to prevent program execution errors.

  • Print
  • Feedback

Resources