Abstract Type
   HOME

TheInfoList



OR:

In
programming languages A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming ...
, an abstract type is a type in a
nominative type system In computer science, a type system is a nominal or nominative type system (or name-based type system) if compatibility and equivalence of data types is determined by explicit declarations and/or the name of the types. Nominal systems are used to d ...
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 Protocol may refer to: Sociology and politics * Protocol (politics), a formal agreement between nation states * Protocol (diplomacy), the etiquette of diplomacy and affairs of state * Etiquette, a code of personal behavior Science and technology ...
'', ''interfaces'', ''signatures'', or ''class types''. In
class-based Class-based programming, or more commonly class-orientation, is a style of object-oriented programming (OOP) in which inheritance (object-oriented programming), inheritance occurs via defining ''class (computer programming), classes'' of object ...
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 pr ...
, abstract types are implemented as ''abstract classes'' (also known as ''
abstract base class In object-oriented programming, a class is an extensible program-code-template for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions or methods). In many languages, the class n ...
es''), and concrete types as ''
concrete class In object-oriented programming, a class is an extensible program-code-template for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions or methods). In many languages, the class ...
es''. 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'', ''
mixin In object-oriented programming languages, a mixin (or mix-in) is a class that contains methods for use by other classes without having to be the parent class of those other classes. How those other classes gain access to the mixin's methods depend ...
s'', ''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 Keyword may refer to: Computing * Keyword (Internet search), a word or phrase typically used by bloggers or online content creator to rank a web page on a particular topic * Index term, a term used as a keyword to documents in an information syst ...
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 List ...
, D or C#. * By including, in the class definition, one or more
abstract method A method in object-oriented programming (OOP) is a Procedure (computer science), procedure associated with a Message passing, message and an Object (computer science), object. An object consists of ''state data'' and ''behavior''; these compose an ...
s (called ''pure virtual functions'' in
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
), which the class is declared to accept as part of its protocol, but for which no implementation is provided. * By
inheriting Inheritance is the practice of receiving private property, titles, debts, entitlements, privileges, rights, and obligations upon the death of an individual. The rules of inheritance differ among societies and have changed over time. Officially ...
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 Alan Ka ...
, 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 Arts, e ...
, 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 NeXTS ...
, 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 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 progra ...
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 progra ...
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 Protocol may refer to: Sociology and politics * Protocol (politics), a formal agreement between nation states * Protocol (diplomacy), the etiquette of diplomacy and affairs of state * Etiquette, a code of personal behavior Science and technolog ...
''; a set of operations that all objects implementing the protocol must support. Abstract types are also an essential part of the
Template Method Pattern In object-oriented programming, the template method is one of the behavioral design patterns identified by Gamma et al. in the book ''Design Patterns''. The template method is a method in a superclass, usually an abstract superclass, and defines ...
.


See also

*
Class Class or The Class may refer to: Common uses not otherwise categorized * Class (biology), a taxonomic rank * Class (knowledge representation), a collection of individuals or objects * Class (philosophy), an analytical concept used differentl ...
*
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 and ...


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 Rosetta Code is a wiki-based programming website with implementations of common algorithms and solutions to various programming problems in many different programming languages. It is named for the Rosetta Stone, which has the same text inscribe ...
Type theory Articles with example Java code sv:Klass (programmering)#Abstrakt klass