Loading AI tools
Feature of a programming language From Wikipedia, the free encyclopedia
In programming languages, an abstract type (also known as existential types)[1] is a type in a nominative type system that cannot be instantiated directly; by contrast, a concrete type can be instantiated directly. Instantiation of an abstract type can occur only indirectly, via a concrete subtype.
This article needs additional citations for verification. (January 2022) |
An abstract type may provide no implementation, or an incomplete implementation. In some languages, abstract types with no implementation (rather than an incomplete implementation) are known as protocols, interfaces, signatures, or class types. In class-based object-oriented programming, abstract types are implemented as abstract classes (also known as abstract base classes), and concrete types as concrete classes. In generic programming, the analogous notion is a concept, which similarly specifies syntax and semantics, but does not require a subtype relationship: two unrelated types may satisfy the same concept.
Often, abstract types will have one or more implementations provided separately, for example, in the form of concrete subtypes that can be instantiated. In object-oriented programming, an abstract class may include abstract methods or abstract properties[2] that are shared by its subclasses. Other names for language features that are (or may be) used to implement abstract types include traits, mixins, flavors, roles, or type classes.[citation needed]
Abstract types may also include any number of non-abstract methods and properties, such as when implementing the Template Method Pattern which uses a mixture of invariant methods with fixed implementations and hook methods which can be overridden in concrete subclasses to provide custonised logic.
Abstract classes can be created, signified, or simulated in several ways:
abstract
in the class definition, as in Java, D or C#.- [NSObject doesNotRecognizeSelector:(SEL)selector]
is invoked upon detection of an unimplemented method).By default, all methods in all classes are concrete, unless the abstract keyword is used. An abstract class may include abstract methods, which have no implementation. By default, all methods in all interfaces are abstract, unless the default keyword is used. The default keyword can be used to specify a concrete method in an interface.
//By default, all methods in all classes are concrete, unless the abstract keyword is used.
public abstract class Demo {
// An abstract class may include abstract methods, which have no implementation.
public abstract int sum(int x, int y);
// An abstract class may also include concrete methods.
public int product(int x, int y) {
return x*y;
}
}
//By default, all methods in all interfaces are abstract, unless the default keyword is used.
interface DemoInterface {
int getLength(); //The abstract keyword can be used here, though is completely useless
//The default keyword can be used in this context to specify a concrete method in an interface
default int product(int x, int y) {
return x * y;
}
}
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.[citation needed]
Some authors argue that classes should be leaf classes (have no subtypes), or else be abstract.[4][5]
Abstract types are useful in that they can be used to define and enforce a protocol; a set of operations that all objects implementing the protocol must support.[citation needed]
Abstract types are also an essential part of the Template Method Pattern.
Seamless Wikipedia browsing. On steroids.
Every time you click a link to Wikipedia, Wiktionary or Wikiquote in your browser's search results, it will show the modern Wikiwand interface.
Wikiwand extension is a five stars, simple, with minimum permission required to keep your browsing private, safe and transparent.