Abstract type

From Wikipedia, the free encyclopedia

This article discusses types with no direct members; see also Abstract data type.

In software engineering, an abstract type is a type in a nominative type system which is declared by the programmer, and which has the property that it contains no members which are not also members of some declared subtype. In many object oriented programming languages, abstract types are known as abstract base classes, interfaces, traits, mixins, flavors, or roles. Note that these names refer to different language constructs which are (or may be) used to implement abstract types.

Contents

[edit] Signifying abstract types

Abstract classes can be created, signified, or simulated in several ways:

  • By use of the explicit keyword abstract in the class definition, as in Java.
  • By including, in the class definition, one or more methods (called pure virtual functions in C++); methods which the class is declared to accept as part of its protocol, but for which no implementation is provided.
  • By inheriting from an abstract type, and not overriding all missing features necessary to complete the class definition.
  • In many dynamically typed languages such as Smalltalk, any class which sends a particular method to this, but doesn't implement that method, can be considered abstract. (However, in many such languages, the error is not detected until the class is used, and the message send results in an error such as doesNotUnderstand).

[edit] Use of abstract types

Abstract types are an important feature in statically typed OO languages. They do not occur in languages without subtyping. Many dynamically typed languages have no equivalent feature (although the use of duck typing makes abstract types unnecessary); however traits are found in some modern dynamically-typed languages.

Many authors argue that classes should be leaf classes (have no subtypes), or else be abstract.

Abstract types are useful in that they can be used to define and enforce a protocol; a set of operations which all objects that implement the protocol must support. The fact that many languages disallow instantiation of abstract types (and force subtypes to implement all needed functionality) further ensures program correctness.

[edit] Types of abstract types

There are several mechanisms for creating abstract types, which vary based on their capability.

  • Full abstract base classes--classes either explicitly declared to be abstract, or which contain abstract (unimplemented) methods, have the same capabilities (apart from instantiation) as a concrete class or type. Full abstract types were present in the earliest versions of C++; and the abstract base class remains the only language construct for generating abstract types in C++.

Due to technical issues with multiple inheritance in C++ and other languages; many OO languages sought to restrict inheritance to a single direct base class. In order to support multiple subtyping, several languages added other features which can be used to create abstract types, but with less power than full-blown classes.

  • Java includes interfaces, an abstract type which may contain method signatures and constants (final variables), but no method implementations or non-final data members. Whereas a class in Java may have inherit from (extend) a single base class; Java classes may implement multiple interfaces.
  • Traits are a more recent approach to the problem, found in Scala and Perl 6 (there known as roles), and proposed as an extension to Smalltalk (wherein the original implementation was developed). Traits are unrestricted in what they include in their definition, and multiple traits may be composed into a class definition. However, the composition rules for traits differ from standard inheritance, to avoid the semantic difficulties often associated with multiple inheritance.

[edit] External links