Jump to content

Abstract type: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
m Reverted 2 edits by 122.176.123.169 identified using STiki
Line 45: Line 45:
===Further reading===
===Further reading===
* {{cite book|title=Head First Java|year=2003|publisher=O'Reilly Media|isbn= 0-596-00920-8|pages=688|url= http://www.headfirstlabs.com/books/hfjava/}}
* {{cite book|title=Head First Java|year=2003|publisher=O'Reilly Media|isbn= 0-596-00920-8|pages=688|url= http://www.headfirstlabs.com/books/hfjava/}}
*Core Java: An Integrated Approach by R. Nageswara Rao


== External links ==
== External links ==

Revision as of 21:10, 13 May 2014

In programming languages, an abstract type is a type in a nominative type system which cannot be instantiated directly. Abstract types are also known as existential types.[1] An abstract type may provide no implementation, or an incomplete implementation. Often, abstract types will have one or more implementations provided separately, for example, in the form of concrete subclasses which can be instantiated. It may include abstract methods or abstract properties[2] that are shared by its subtypes.

The object oriented form of abstract types are known as abstract base classes or simply abstract classes. In some languages, abstract types with no implementation are known as protocols, interfaces, signatures, class types. Other names for language features that are (or may be) used to implement abstract types include traits, mixins, flavors, roles, or type classes.

A type that is not abstract is called a concrete type (or concrete class).

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, D or C#.
  • By including, in the class definition, one or more abstract 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, like Objective-C, the error is not detected until the class is used, and the message returns results in an exception error message such as "Does not recognize selector: xxx" as - [NSObject doesNotRecognizeSelector:(SEL)selector] is invoked upon detection of an unimplemented method).

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 OOP languages. 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.

Some authors argue that classes should be leaf classes (have no subtypes), or else be abstract.[3][4]

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.

References

  1. ^ Mitchell, John C.; Plotkin, Gordon D.; Abstract Types Have Existential Type, ACM Transactions on Programming Languages and Systems, Vol. 10, No. 3, July 1988, pp. 470–502
  2. ^ http://download.oracle.com/javase/tutorial/java/IandI/abstract.html
  3. ^ Riel, Arthur (1996). Object-Oriented Design Heuristics. Addison-Wesley Professional. p. 89. ISBN 0-201-63385-X.
  4. ^ Meyers, Scott (1996). More Effective C++. Addison-Wesley Professional. p. 258. ISBN 020163371x. Make non-leaf classes abstract

Further reading