Abstract type

Revision as of 09:52, 20 November 2008 by rosettacode>Dmitry-kazakov (Ttypes without instances)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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.

Task
Abstract type
You are encouraged to solve this task according to the task description, using any language you may know.

Because the abstract type's implementation is incomplete, if any, it makes no sense to have any instances (objects) of it. Therefore an OO language would prevent creation of such objects.

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.

Ada

Interface

Interfaces in Ada may have no components or implemented operation except for ones implemented as null operations. Interfaces can be multiply inherited. <ada> type Queue is limited interface; procedure Enqueue (Lounge : in out Queue; Item : in out Element) is abstract; procedure Dequeue (Lounge : in out Queue; Item : in out Element) is abstract; </ada> Interfaces can be declared synchronized or task when intended implementations are to be provided by protected objects or tasks. For example: <ada> type Scheduler is task interface; procedure Plan (Manager : in out Scheduler; Activity : in out Job) is abstract; </ada>

Abstract type

Abstract types may provide components and implementation of their operations. Abstract types are singly inherited. <ada> with Ada.Finalization.Limited_Controlled; ... type Node is abstract new Ada.Finalization.Limited_Controlled and Queue with record

  Previous : not null access Node'Class := Node'Unchecked_Access;
  Next     : not null access Node'Class := Node'Unchecked_Access;

end record; overriding procedure Finalize (X : in out Node); -- Removes the node from its list if any overriding procedure Dequeue (Lounge : in out Queue; Item : in out Element); overriding procedure Enqueue (Lounge : in out Queue; Item : in out Element); procedure Process (X : in out Node) is abstract; -- To be implemented </ada> Here Node is an abstract type that is inherited from Limited_Controlled and implements a node of a doubly linked list. It also implements the interface of a queue described above, because any node can be considered a head of the queue of linked elements. For the operation Finalize an implementation is provided to ensure that the element of a list is removed from there upon its finalization. The operation itself is inherited from the parent type Limited_Controlled and then overridden. The operations Dequeue and Enqueue of the Queue interface are also implemented.