Abstract type: Difference between revisions

Content added Content deleted
m (Link to inheritance added)
No edit summary
Line 36:
</ada>
Here Node is an abstract type that is inherited from Limited_Controlled and implements a node of a [[Doubly-Linked List (element) | 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.
 
 
=={{header|Python}}==
 
<python>
class BaseQueue(object):
"""Abstract/Virtual Class
"""
def __init__(self):
self.contents = list()
raise NotImplementedError
def Enqueue(self, item):
raise NotImplementedError
def Dequeue(self):
raise NotImplementedError
</python>
 
Python allows multiple inheritance and it's more common to implement "mix-in" classes rather than abstract interfaces. (Mix-in classes can implement functionality as well define interaces).
 
In this example we're simply following the Python convention of raising the built-in "NotImplementedError" for each function which must be implemented by our subclasses. This is a "purely virtual" class because all of its methods raise the exception. (It is sufficient for __init__ to do so for any partial virtual abstractions since that still ensures that the exception will be raised if anyone attempts to instantiate the base/abstract class directly rather than one of it's concrete descendents).
 
The method signatures and the instantiation of a "contents" list shown here can be viewed as documentary hints to anyone inheriting from this class. They won't actually do anything in the derived classes (since these methods must be over-ridden therein).
 
:'''Note:''' This "BaseQueue" example should not be confused with Python's standard library Queue class. That is used as the principle "producer/consumer" communications mechanism among threads (and newer ''multiprocessing'' processes).