- Abstract type
: "This article discusses types with no direct members; see also
Abstract data type ."Insoftware engineering , an abstract type is a type in anominative type system which is declared by the programmer, and which has the property that it contains no members which are also not members of some declaredsubtype . In manyobject oriented programming languages, abstract types are known as abstract base classes, interfaces, traits,mixin s, 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 purevirtual functions inC++ ); 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 asSmalltalk , 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 asdoesNotUnderstand ).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 class es (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 ofC++ ; 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 withmultiple inheritance in C++ and other languages; many OO languages sought to restrict inheritance to a single direct base class. In order to support multiplesubtyping , 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 includesmixin s, based on the Flavors system developed byDavid Moon forLisp Machine Lisp . (It should be noted that CLOS usesgeneric function s, defined apart from classes, rather thanmember function s 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 inScala andPerl 6 (there known as "roles"), and proposed as an extension toSmalltalk (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
* "Types and Programming Languages" by Benjamin Pierce (MIT Press 2002) [http://www.cis.upenn.edu/~bcpierce/tapl/main.html]
* "More Effective C++: 35 New Ways to Improve Your Programs and Designs" byScott Meyers (1995 ) ISBN 0-201-63371-X
* [http://web.cecs.pdx.edu/~black/publications/TR_CSE_02-012.pdf Traits: Composable Units of Behavior] by Nathanael Schärli, Stéphane Ducasse, Oscar Nierstrasz and Andrew Black
Wikimedia Foundation. 2010.