Pure Virtual Class
   HOME

TheInfoList



OR:

In
programming languages A programming language is a system of notation for writing computer programs. Programming languages are described in terms of their syntax (form) and semantics (meaning), usually defined by a formal language. Languages usually provide features ...
, an abstract type (also known as existential types) is a
type Type may refer to: Science and technology Computing * Typing, producing text via a keyboard, typewriter, etc. * Data type, collection of values used for computations. * File type * TYPE (DOS command), a command to display contents of a file. * ...
in a
nominative type system In computer science, a type system is nominal (also called nominative or name-based) if compatibility and equivalence of data types is determined by explicit declarations and/or the name of the types. Nominal systems are used to determine whether ...
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 (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 occurs via defining ''classes'' of objects, instead of inheritance occurring via the objects alone (compare prototyp ...
object-oriented programming Object-oriented programming (OOP) is a programming paradigm based on the concept of '' objects''. Objects can contain data (called fields, attributes or properties) and have actions they can perform (called procedures or methods and impl ...
, abstract types are implemented as '' abstract classes'' (also known as '' abstract base classes''), and concrete types as ''
concrete class In object-oriented programming, a class defines the shared aspects of objects created from the class. The capabilities of a class differ between programming languages, but generally the shared aspects consist of state ( variables) and behavior ( ...
es''. In
generic programming Generic programming is a style of computer programming in which algorithms are written in terms of data types ''to-be-specified-later'' that are then ''instantiated'' when needed for specific types provided as parameters. This approach, pioneer ...
, 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 method A method in object-oriented programming (OOP) is a procedure associated with an object, and generally also a message. An object consists of ''state data'' and ''behavior''; these compose an ''interface'', which specifies how the object may be u ...
s'' or ''abstract
properties Property is the ownership of land, resources, improvements or other tangible objects, or intellectual property. Property may also refer to: Philosophy and science * Property (philosophy), in philosophy and logic, an abstraction characterizing an ...
'' 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''. Abstract types may also include any number of non-abstract methods and properties, such as when implementing the
Template Method Pattern In object-oriented programming, the template method is one of the behavioral pattern, behavioral Software design pattern, design patterns identified by Gamma et al. in the book ''Design Patterns''. The template method is a method in a superclas ...
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 Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
, D or C#. * By including, in the class definition, one or more
abstract method A method in object-oriented programming (OOP) is a procedure associated with an object, and generally also a message. An object consists of ''state data'' and ''behavior''; these compose an ''interface'', which specifies how the object may be u ...
s (called ''pure
virtual functions In object-oriented programming such as is often used in C++ and Object Pascal, a virtual function or virtual method is an inheritable and overridable function or method that is dispatched dynamically. Virtual functions are an important part o ...
'' 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 does not implement all abstract methods from its parent becomes abstract itself. * In many dynamically typed languages such as
Smalltalk Smalltalk is a purely object oriented programming language (OOP) that was originally created in the 1970s for educational use, specifically for constructionist learning, but later found use in business. It was created at Xerox PARC by Learni ...
, any class that sends a particular method to this, but does not implement that method, can be considered abstract. (However, in many such languages, like
Objective-C Objective-C is a high-level general-purpose, object-oriented programming language that adds Smalltalk-style message passing (messaging) to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was ...
, 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).


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 In computer programming, a type system is a logical system comprising a set of rules that assigns a property called a ''type'' (for example, integer, floating point, string) to every '' term'' (a word, phrase, or other set of symbols). Usu ...
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'' (for example, integer, floating point, string) to every '' term'' (a word, phrase, or other set of symbols). Usua ...
languages have no equivalent feature (although the use of
duck typing In computer programming, duck typing 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 ...
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 In object-oriented programming, the template method is one of the behavioral pattern, behavioral Software design pattern, design patterns identified by Gamma et al. in the book ''Design Patterns''. The template method is a method in a superclas ...
.


See also

*
Class Class, Classes, 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 d ...
*
Concept A concept is an abstract idea that serves as a foundation for more concrete principles, thoughts, and beliefs. Concepts play an important role in all aspects of cognition. As such, concepts are studied within such disciplines as linguistics, ...
*
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 Rosetta Code is a wiki-based programming chrestomathy website with implementations of common algorithms and solutions to various computer programming, programming problems in many different programming languages. It is named for the Rosetta Stone ...
Type theory Articles with example Java code sv:Klass (programmering)#Abstrakt klass