Abstract type
In programming languages, an abstract type is a type in a nominative type system that cannot be instantiated directly; by contrast, a concrete type be instantiated directly. Instantiation of an abstract type can occur only indirectly, via a concrete subtype.
An abstract type may provide no implementation, or an incomplete implementation. In some languages, abstract types with no implementation are known as protocols, interfaces, signatures, or class types. In class-based object-oriented programming, abstract types are implemented as abstract 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 be instantiated. In object-oriented programming, an abstract class may include abstract methods or abstract properties that are shared by its subclasses. Other names for language features that are used to implement abstract types include traits, mixins, flavors, roles, or type classes.
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.
Creation
Abstract classes can be created, signified, or simulated in several ways:- By use of the explicit keyword in the class definition, as in Java, D or C#.
- By including, in the class definition, one or more abstract 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 other words, a child type that does not implement all abstract methods from its parent becomes abstract itself.
- In many dynamically typed languages such as Smalltalk, any class that sends a particular method to this, but does not implement that method, can be considered abstract. .
Examples
Java
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
//By default, all methods in all interfaces are abstract, unless the default keyword is used.
interface DemoInterface
Usage
Abstract types are an important feature in statically typed OOP languages. Many dynamically typed languages have no equivalent feature ; however traits are found in some modern dynamically-typed languages.Some authors argue that classes should be leaf classes, or else be abstract.
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.
Abstract types are also an essential part of the template method pattern.