Abstract type: Difference between revisions

Content added Content deleted
(added c++)
(!= ocaml abstract type)
Line 2: Line 2:
[[Category:Encyclopedia]]
[[Category:Encyclopedia]]
[[Category:Object oriented]]
[[Category:Object oriented]]
'''Abstract type''' is a type without instances. In [[object-oriented programming]] abstract types are used for partial implementation of other types, which are later derived from it. Abstract type may provide implementation of some operations and/or components. Abstract types without any implementation are called '''interfaces'''. In the languages that do not support multiple [[inheritance]] ([[Ada]], [[Java]]), interfaces are usually allowed to be multiply inherited from. The languages with multiple inheritance (like [[C++]]) usually make no distinction between partially implementable abstract types and interfaces.


'''Abstract type''' is a type without instances or without definition.
Because the abstract type's implementation is incomplete, if any, it makes no sense to have any instances (objects) of it. Therefore an [[object-oriented programming | OO]] language would prevent creation of such objects.

For example in [[object-oriented programming]] abstract types can be in some languages a partial implementation of other types, which are later derived from it. Abstract type may provide implementation of some operations and/or components. Abstract types without any implementation are called '''interfaces'''. In the languages that do not support multiple [[inheritance]] ([[Ada]], [[Java]]), interfaces are usually allowed to be multiply inherited from. The languages with multiple inheritance (like [[C++]]) usually make no distinction between partially implementable abstract types and interfaces. Because the abstract type's implementation is incomplete, if any, it makes no sense to have any instances (objects) of it. Therefore an [[object-oriented programming | OO]] language would prevent creation of such objects.

In strongly statically typed languages, abstract types can be used for example to hide the implementation of a type in the interface of a module. So both the complexity is hidden for the user of the module, and also the implementation can change without repercutions for source code that use it. Abstract types that even have no definition in the implementation can also be used for the type algebra.


'''Task''': show how an abstract type can be declared in the language. If the language makes a distinction between interfaces and partially implemented types illustrate both.
'''Task''': show how an abstract type can be declared in the language. If the language makes a distinction between interfaces and partially implemented types illustrate both.
Line 63: Line 66:
int add(int a, int b);
int add(int a, int b);
}</java>
}</java>

=={{header|OCaml}}==

===Virtual===

The equivalent of what is called abstract type in the other OO examples of this page is just called '''virtual''' in Objective Caml to define '''virtual methods''' and '''virtual classes''':

<ocaml>class virtual foo =
object
method virtual bar : int
end</ocaml>

===Abstract Type===

In OCaml what we call an abstract type is not OO related, it is only a type defined without definition, for example:
<ocaml>type t</ocaml>
it is used for example to hide an implementation from the interface of a module or for type algebra.


=={{header|Python}}==
=={{header|Python}}==