Jump to content

Abstract type: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
m Interwiki sv
Line 8: Line 8:
Abstract classes can be created, signified, or simulated in several ways:
Abstract classes can be created, signified, or simulated in several ways:
* By use of the explicit [[keyword (computer programming)| keyword]] <tt>abstract</tt> in the class definition, as in [[Java (programming language)|Java]].
* By use of the explicit [[keyword (computer programming)| keyword]] <tt>abstract</tt> in the class definition, as in [[Java (programming language)|Java]].
* By including, in the class definition, one or more [[abstract method|method]]s (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 including, in the class definition, one or more [[abstract method|method]]s (called '''pure [[virtual functions]]''' in [[C++]]); which the class is declared to accept as part of its protocol, but for which no implementation is provided.
* By [[inheritance (computer science)|inheriting]] from an abstract type, and not overriding all missing features necessary to complete the class definition.
* By [[inheritance (computer science)|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 (computer science)|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]]).
* In many dynamically typed languages such as [[Smalltalk]], any class which sends a particular method to [[this (computer science)|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]]).

Revision as of 10:37, 21 January 2009

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 members which are 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.

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++); 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).

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.

Types of abstract types

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

  • Full abstract base classes are classes either explicitly declared to be abstract, or which contain abstract (unimplemented) methods. Except the instantiation capability, they have the same capabilities 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++. A class having only pure virtual methods is often called a pure virtual class; it is necessarily abstract.
    • Note: 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
  • Common Lisp Object System includes mixins, based on the Flavors system developed by David Moon for Lisp Machine Lisp. (It should be noted that CLOS uses generic functions, defined apart from classes, rather than member functions defined within the class).
  • Java includes interfaces, an abstract type which may contain method signatures and constants (final variables), but no method implementations or non-final data members. Java classes may "implement" multiple interfaces. An abstract class in Java may implement interfaces and define some method signatures while keeping other methods abstract with the "abstract" keyword.
  • 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.

External links