In
programming languages
A programming language is a system of notation for writing computer program, computer programs. Most programming languages are text-based formal languages, but they may also be visual programming language, graphical. They are a kind of computer ...
, an abstract type is a type in a
nominative type system that cannot be
instantiated directly; a type that is not abstract – which ''can'' be instantiated – is called a ''concrete type''. Every instance of an abstract type is an instance of some concrete
subtype
Subtype may refer to:
* Viral subtypes, such as Subtypes of HIV
* Subtyping
In programming language theory, subtyping (also subtype polymorphism or inclusion polymorphism) is a form of type polymorphism in which a subtype is a datatype that is ...
. Abstract types are also known as ''existential types''.
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
Object-oriented programming (OOP) is a programming paradigm based on the concept of " objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of ...
, abstract types are implemented as ''abstract classes'' (also known as ''
abstract base classes''), and concrete types as ''
concrete classes''. In
generic programming
Generic programming is a style of computer programming in which algorithms are written in terms of types ''to-be-specified-later'' that are then ''instantiated'' when needed for specific types provided as parameters. This approach, pioneered b ...
, the analogous notion is a
concept
Concepts are defined as abstract ideas. They are understood to be the fundamental building blocks of the concept behind principles, thoughts and beliefs.
They play an important role in all aspects of cognition. As such, concepts are studied by s ...
, 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''
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''.
Signifying abstract types
Abstract classes can be created, signified, or simulated in several ways:
* By use of the explicit
keyword in the class definition, as in
Java
Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's mo ...
,
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 other words, a child type that doesn't implement all abstract methods from its parent becomes abstract itself.
* In many dynamically typed languages such as
Smalltalk
Smalltalk is an object-oriented, dynamically typed reflective programming language. It was designed and created in part for educational use, specifically for constructionist learning, at the Learning Research Group (LRG) of Xerox PARC by ...
, any class that sends a particular method to
this
This may refer to:
* ''This'', the singular proximal demonstrative pronoun
Places
* This, or ''Thinis'', an ancient city in Upper Egypt
* This, Ardennes, a commune in France
People with the surname
* Hervé This, French culinary chemist Art ...
, but doesn't implement that method, can be considered abstract. (However, in many such languages, like
Objective-C
Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was selected by NeXT for its N ...
, 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
- SObject doesNotRecognizeSelector:(SEL)selector/code> is invoked upon detection of an unimplemented method).
Example (Java)
//By default, all methods in all classes are concrete, unless the abstract keyword is used.
abstract class Demo
//By default, all methods in all interfaces are abstract, unless the default keyword is used.
interface DemoInterface
Use of abstract types
Abstract types are an important feature in statically typed OOP languages. Many dynamically typed
In computer programming, a type system is a logical system comprising a set of rules that assigns a property called a type to every "term" (a word, phrase, or other set of symbols). Usually the terms are various constructs of a computer program ...
languages have no equivalent feature (although the use of duck typing
Duck typing in computer programming is an application of the duck test—"If it walks like a duck and it quacks like a duck, then it must be a duck"—to determine whether an object can be used for a particular purpose. With nominative t ...
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.
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.
See also
* Class
* Concept
Concepts are defined as abstract ideas. They are understood to be the fundamental building blocks of the concept behind principles, thoughts and beliefs.
They play an important role in all aspects of cognition. As such, concepts are studied by s ...
* Type class
In computer science, a type class is a type system construct that supports ad hoc polymorphism. This is achieved by adding constraints to type variables in parametrically polymorphic types. Such a constraint typically involves a type class T a ...
References
Further reading
* {{cite book, title=Head First Java, year=2003, publisher=O'Reilly Media, isbn=0-596-00920-8, page
688
url=https://archive.org/details/headfirstjava00sier_0/page/688
*Core Java: An Integrated Approach by R. Nageswara Rao
External links
* "Abstract or Skeletal Interfaces Explained
* ''Types and Programming Languages'' by Benjamin Pierce (MIT Press 2002
*
Abstract type
' at Rosetta Code
Type theory
Articles with example Java code
sv:Klass (programmering)#Abstrakt klass