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

Page 2 of 4

Java's specification as a strongly typed language enables the static determination of all variable and program expression types. That allows most type enforcement to occur at compile time. A static type-checker, usually the language compiler, ingests the program text and verifies that variables and program expressions conform to the typing rules.

User-defined data types offer further benefit by providing valuable insight into program meaning. The declaration of types and their use in a program give a partial program specification. Like descriptive variable names, type declarations reveal clues to system structure.

Defining types

The Java language provides two separate mechanisms for declaring user-defined types. A class defines a type with a partial or full implementation, whereas an interface defines a type free of any implementation concerns.

Defining types with classes

Most programmers think of Java classes as primarily code implementation repositories. A class text defines an implementation template that effectively becomes an object-creation blueprint. This important aspect of the class concept reflects the manner in which Java facilitates code modularization. Classes provide the primary unit for decomposing a software system into understandable and manageable pieces. In turn, that modularization significantly aids the comprehensibility of larger systems.

Class inheritance through the Java extends clause further facilitates modularization. Module, or implementation, inheritance offers a way to create subclasses that automatically utilize implementation code from the superclass module. Subclasses build on superclass implementation and thus provide a means for module extension. This implementation-centric view of inheritance emphasizes code reuse.

Java classes also play an additional significant role. As well as providing implementation code, every class also defines a type. We often speak of this type as the public interface to the class. That terminology is somewhat confusing in light of the Java language interface keyword. Regardless, the set of public methods declared by the class defines a new type. This new type enjoys all the privileges and benefits of the Java type system in a manner that is completely independent of implementation code. Whether concrete or abstract, a class always defines a single new type.

Inheritance via the extends clause also plays another important role that mirrors a class's ability to define a type. Type, or interface, inheritance provides a mechanism for creating subtypes that build on a supertype's public interface. Since a type B declared as a subtype of A also has type A, the type-checker permits any variable or program expression of type B when expecting a variable or program expression of type A.

As an example, consider the following definition of class Base:

public class Base
{
  public String m1()
  {
    return "Base.m1()";
  }
  public String m2( String s )
  {
    return "Base.m2(" + s + ")";
  }
  private String p()
  {
    return "Base.p()";
  }
}


The class provides implementation for three methods: m1(), m2(String), and p(). Only public method declarations become part of the defined type, so type Base consists of operations m1() and m2(String). (For this discussion, I'll ignore the implementation and type operations inherited from the primordial Object class.) Type Base does not include the private method p(), thus the type-checker rejects the following code:

  • Print
  • Feedback

Resources