Abstract type
Encyclopedia
In programming languages, an abstract type is a type in a nominative type system
Nominative type system
In computer science, a nominal or nominative type system is a major class of type system, in which compatibility and equivalence of data types is determined by explicit declarations and/or the name of the types. Nominative systems are used to determine if types are equivalent, as well as if a type...

 which cannot be instantiated. (However, it may have concrete subtypes that do have instances.) An abstract type may have no implementation, or an incomplete implementation. It may include abstract methods or abstract properties that are shared by its subtypes.

A type that is not abstract is called a concrete type.

In many object oriented programming languages, abstract types are known as abstract base classes. In some languages, abstract types with no implementation are known as interfaces
Interface (computer science)
In the field of computer science, an interface is a tool and concept that refers to a point of interaction between components, and is applicable at the level of both hardware and software...

. Other names for languages features that are (or may be) used to implement abstract types include traits, mixin
Mixin
In object-oriented programming languages, a mixin is a class that provides a certain functionality to be inherited or just reused by a subclass, while not meant for instantiation , Mixins are synonymous functionally with abstract base classes...

s
, flavors, or roles.

Signifying abstract types

Abstract classes can be created, signified, or simulated in several ways:
  • By use of the explicit keyword
    Keyword (computer programming)
    In computer programming, a keyword is a word or identifier that has a particular meaning to the programming language. The meaning of keywords — and, indeed, the meaning of the notion of keyword — differs widely from language to language....

     abstract in the class definition, as in Java
    Java (programming language)
    Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

    , D
    D (programming language)
    The D programming language is an object-oriented, imperative, multi-paradigm, system programming language created by Walter Bright of Digital Mars. It originated as a re-engineering of C++, but even though it is mainly influenced by that language, it is not a variant of C++...

     or C#.
  • By including, in the class definition, one or more abstract methods (called pure virtual functions in C++
    C++
    C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...

    ), which the class is declared to accept as part of its protocol, but for which no implementation is provided.
  • By inheriting
    Inheritance (computer science)
    In object-oriented programming , inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support...

     from an abstract type, and not overriding all missing features necessary to complete the class definition.
  • In many dynamically typed languages such as Smalltalk
    Smalltalk
    Smalltalk is an object-oriented, dynamically typed, reflective programming language. Smalltalk was created as the language to underpin the "new world" of computing exemplified by "human–computer symbiosis." It was designed and created in part for educational use, more so for constructionist...

    , any class which sends a particular method to this
    This (computer science)
    In many object-oriented programming languages, this is a keyword that is used in instance methods to refer to the object on which they are working. C++ and languages which derive in style from it generally use 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 returns results in an exception error message such as "does Not Understand").

Example (Java)


abstract class Demo {
// An abstract class may include abstract methods, which have no implementation.
abstract public int sum(int x, int y);

// An abstract class may also include concrete methods.
public int product(int x, int y) { return x*y; }
}

interface DemoInterface {
// All methods in an interface are abstract.
int getLength;
}

Use of abstract types

Abstract types are an important feature in statically typed OO languages. Many dynamically typed languages have no equivalent feature (although the use of duck typing
Duck typing
In computer programming with object-oriented programming languages, duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface...

 makes abstract types unnecessary); however traits are found in some modern dynamically-typed languages.

Some authors argue that classes should be leaf class
Leaf class
In class-based object-oriented programming languages, a leaf class is a class that should not be subclassed. This can be enforced either by convention, or by using a language feature such as the final keyword in Java or the sealed keyword in C#....

es (have no subtypes), or else be abstract.

Abstract types are useful in that they can be used to define and enforce a protocol
Protocol (object-oriented programming)
In object-oriented programming, a protocol or interface is a common means for unrelated objects to communicate with each other. These are definitions of methods and values which the objects agree upon in order to cooperate....

; a set of operations which all objects that implement the protocol must support.

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 by Scott Meyers
    Scott Meyers
    Scott Douglas Meyers is an American author and software consultant, specializing in the C++ computer programming language. He is known for his Effective C++ book series. He is a frequent speaker at conferences and trade shows. He holds a Ph.D. in computer science from Brown University and M.S...

     (1995) ISBN 0-201-63371-X
  • Traits: Composable Units of Behavior by Nathanael Schärli, Stéphane Ducasse, Oscar Nierstrasz and Andrew Black
  • Abstract type at Rosetta Code
    Rosetta Code
    Rosetta Code is a wiki-based programming chrestomathy website with solutions to various programming problems in many different programming languages. It was created in 2007 by Mike Mol. Rosetta Code includes 450 programming tasks, and covers 351 programming languages...

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK